MIPS64 user mode emulation in QEMU
v4 cover MIPS64 user mode emulation in QEMU
From: Khansa Butt <[email protected]>
[sorry version missed in previous set]
This is the team work of Ehsan-ul-Haq, Abdul Qadeer, Abdul Waheed, Khansa Butt from HPCN Lab KICS UET Lahore.
In previous patch set we were including Cavium specific instructions along with Cavium specifc registers in UME. Because of these register fields we had to bump the cpu version up but I noticed that cpu_save() and cpu_load() are not called in UME so we decided to postpone Octeon specific changes ( registers and instructions) and will include them in our SME Patches.
MIPS64el is included in Andreas Färber patch series so I have not include it in our patches (we are working on mips64el but mips64el binary is needed)
v3-->v4
1)Drop Cavium specific instructions and save/load of Cavium's registers
2) signal handling of mips64 has been changed according to Andreas Färber and Richard Henderson v2-->v3
1)SEQI and SEQ related changes
2)Cavium specific change in set_thread_area syscall has been removed
as it corresponds to modified libc and kernel.
v1-->v2
1)Fix issues related to coding style, typos and misleading comments
2)Addition of helper functions for sum of Cavium specific instructions
v1 contains
1)MIPS64 user mode emulation in QEMU
2) Octeon CPU definition + Cavium Specific Instructions
linux-user: Add support for MIPS64
target-mips: Enable 64 bit user mode
target-mips: Fix FPU reset
linux-user: Implement signal handling for mips64
configure | 1 + default-configs/mips64-linux-user.mak | 1 + linux-user/main.c | 21 +++- linux-user/mips64/syscall.h | 2 + linux-user/signal.c | 272 +++++++++++++++++---------------- target-mips/translate.c | 4 + 6 files changed, 169 insertions(+), 132 deletions(-) create mode 100644 default-configs/mips64-linux-user.mak
Signature · 1 line
1.7.3.4
PATCH v4 1/4 linux-user: Add support for MIPS64
From: Khansa Butt <[email protected]>
This patch incorporates
1)default configs for mips64
2)updated target-list with mips64-linux-user
3)syscall differences: Linux 64-bit syscalls have different number range and because of n64 ABI, 8 argument registers are suffient for do_syscall() 4)define TARGET_QEMU_ESIGRETURN for mips64
--- configure | 1 + default-configs/mips64-linux-user.mak | 1 + linux-user/main.c | 21 +++++++++++++++++++-- linux-user/mips64/syscall.h | 2 ++ 4 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 default-configs/mips64-linux-user.mak
configure +1 -0
@@ -915,6 +915,7 @@ microblaze-linux-user \ microblazeel-linux-user \ mips-linux-user \ mipsel-linux-user \ +mips64-linux-user \ ppc-linux-user \ ppc64-linux-user \ ppc64abi32-linux-user \
default-configs/mips64-linux-user.mak new file +1 -0
@@ -0,0 +1 @@ +# Default configuration for mips64-linux-user
linux-user/main.c +19 -2
@@ -2157,7 +2157,8 @@ static int do_store_exclusive(CPUMIPSState *env)void cpu_loop (CPUMIPSState *env) {target_siginfo_t info; -int trapnr, ret; +int trapnr; +abi_long ret;unsigned int syscall_num;for (;;) {
@@ -2166,8 +2167,23 @@ void cpu_loop(CPUMIPSState *env)cpu_exec_end (env);switch (trapnr) {case EXCP_SYSCALL : - syscall_num = env->active_tc.gpr[2 ] -4000 ; env->active_tc.PC +=4 ; +#if defined(TARGET_ABI_MIPSN64 ) + syscall_num = env->active_tc.gpr[2 ] -5000 ; +/* MIPS64 has eight argument registers so there is +* no need to get arguments from stack +*/ + ret =do_syscall (env, env->active_tc.gpr[2 ], + env->active_tc.gpr[4 ], + env->active_tc.gpr[5 ], + env->active_tc.gpr[6 ], + env->active_tc.gpr[7 ], + env->active_tc.gpr[8 ], + env->active_tc.gpr[9 ], + env->active_tc.gpr[10 ], + env->active_tc.gpr[11 ]); +#else + syscall_num = env->active_tc.gpr[2 ] -4000 ;if (syscall_num >=sizeof (mips_syscall_args)) { ret = -TARGET_ENOSYS ; }else {
@@ -2205,6 +2221,7 @@ void cpu_loop(CPUMIPSState *env) env->active_tc.gpr[7 ], arg5, arg6, arg7, arg8); } +#endif done_syscall:if (ret == -TARGET_QEMU_ESIGRETURN ) {/* Returning from a successful sigreturn syscall.
linux-user/mips64/syscall.h +2 -0
@@ -218,4 +218,6 @@ struct target_pt_regs { +#define TARGET_QEMU_ESIGRETURN 255 +#define UNAME_MACHINE "mips64"
Signature · 1 line
1.7.3.4
PATCH v4 2/4 target-mips: Enable 64 bit user mode
In reply to [email protected]From: Khansa Butt <[email protected]>
MIPS_HFLAG_UX is included in env->hflags so that the address computation
for LD instruction does not get treated as 32 bit code, see gen_op_addr_add() in translate.c.
--- target-mips/translate.c | 3 +++ 1 files changed, 3 insertions(+), 0 deletions(-)
target-mips/translate.c +3 -0
@@ -12773,6 +12773,9 @@ void cpu_reset (CPUMIPSState *env)#if defined(CONFIG_USER_ONLY ) env->hflags =MIPS_HFLAG_UM ; +#ifdef TARGET_MIPS64 + env->hflags |=MIPS_HFLAG_UX ; +#endif /* Enable access to the SYNCI_Step register. */ env->CP0_HWREna |= (1 <<1 );if (env->CP0_Config1 & (1 <<CP0C1_FP )) {
Signature · 1 line
1.7.3.4
PATCH v4 3/4 target-mips: Fix FPU reset
In reply to [email protected]From: Khansa Butt <[email protected]>
in translate_init.c fpu_init() initializes active_fpu for given cpu model afterwards cpu_reset() resets all the members upto breakpoints due to this whatever the value of cpu_model->CR1_fcr0 was , the value of env->active_fpu.fcr0 is zero now. This patch resolve this issue so that FP operations can be emulated. cpu_reset() can't call fpu_init() as it require to pass mips_def_t *def to cpu_reset() which will break the convention
--- target-mips/translate.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
target-mips/translate.c +1 -0
@@ -12731,6 +12731,7 @@ void cpu_reset (CPUMIPSState *env)tlb_flush (env,1 );/* Reset registers to their default values */ + env->active_fpu.fcr0 = env->cpu_model->CP1_fcr0; env->CP0_PRid = env->cpu_model->CP0_PRid; env->CP0_Config0 = env->cpu_model->CP0_Config0;#ifdef TARGET_WORDS_BIGENDIAN
Signature · 1 line
1.7.3.4
PATCH v4 4/4 linux-user: Implement signal handling for mips64
In reply to [email protected]From: Khansa Butt <[email protected]>
This patch includes sigcontext for mips64 suggested by Andreas Färber and sa_flags byte swap fix by Richard Henderson setup_frame() and do_sigreturn need non RT syscall numbers which do not exist in linux 64 bit syscalls. so both function should have different implementation for mips64. Actual signal.c of kernel does not use signal trampoline.
Other related functions have been put under #if defined(TARGET_MIPS).
--- linux-user/signal.c | 272 +++++++++++++++++++++++++++------------------------ 1 files changed, 142 insertions(+), 130 deletions(-)
linux-user/signal.c +142 -130
@@ -587,7 +587,11 @@ int do_sigaction(int sig, const struct target_sigaction *act,#endif if (oact) { oact->_sa_handler =tswapal (k->_sa_handler); - oact->sa_flags =tswapal (k->sa_flags); +#if defined(TARGET_MIPS ) + oact->sa_flags =bswap32 (k->sa_flags); +#else + oact->sa_flags =tswapal (act->sa_flags); +#endif #if !defined(TARGET_MIPS ) oact->sa_restorer =tswapal (k->sa_restorer);#endif
@@ -596,7 +600,11 @@ int do_sigaction(int sig, const struct target_sigaction *act,if (act) {/* FIXME: This is not threadsafe. */ k->_sa_handler =tswapal (act->_sa_handler); +#if defined(TARGET_MIPS ) + k->sa_flags =bswap32 (act->sa_flags); +#else k->sa_flags =tswapal (act->sa_flags); +#endif #if !defined(TARGET_MIPS ) k->sa_restorer =tswapal (act->sa_restorer);#endif
@@ -2414,35 +2422,6 @@ void sparc64_get_context(CPUSPARCState *env) force_sig(TARGET_SIGSEGV); }#endif -#elif defined(TARGET_ABI_MIPSN64) - -# warning signal handling not implemented - -static void setup_frame (int sig,struct target_sigaction *ka, -target_sigset_t *set,CPUState *env) -{ -fprintf (stderr,"setup_frame: not implemented\n" ); -} - -static void setup_rt_frame (int sig,struct target_sigaction *ka, -target_siginfo_t *info, -target_sigset_t *set,CPUState *env) -{ -fprintf (stderr,"setup_rt_frame: not implemented\n" ); -} - -long do_sigreturn (CPUState *env) -{ -fprintf (stderr,"do_sigreturn: not implemented\n" ); -return -TARGET_ENOSYS ; -} - -long do_rt_sigreturn (CPUState *env) -{ -fprintf (stderr,"do_rt_sigreturn: not implemented\n" ); -return -TARGET_ENOSYS ; -} -#elif defined(TARGET_ABI_MIPSN32)# warning signal handling not implemented
@@ -2472,7 +2451,9 @@ long do_rt_sigreturn(CPUState *env)return -TARGET_ENOSYS ; } -#elif defined(TARGET_ABI_MIPSO32 ) +#elif defined(TARGET_MIPS ) + +#if defined(TARGET_ABI_MIPSO32 ) struct target_sigcontext { uint32_t sc_regmask; /* Unused */
@@ -2496,6 +2477,136 @@ struct target_sigcontext {target_ulong sc_lo3; }; +/* compare linux/arch/mips/kernel/signal.c:setup_frame() */ +static void setup_frame (int sig,struct target_sigaction *ka, +target_sigset_t *set,CPUState *regs) +{ +struct sigframe *frame; +abi_ulong frame_addr; +int i; + + frame_addr =get_sigframe (ka, regs,sizeof (*frame)); +if (!lock_user_struct (VERIFY_WRITE , frame, frame_addr,0 )) + goto give_sigsegv; + +install_sigtramp (frame->sf_code, TARGET_NR_sigreturn); + +if (setup_sigcontext (regs, &frame->sf_sc)) + goto give_sigsegv; + +for (i =0 ; i <TARGET_NSIG_WORDS ; i++) { +if (__put_user (set->sig[i], &frame->sf_mask.sig[i])) + goto give_sigsegv; + } + +/* +* Arguments to signal handler: +* +* a0 = signal number +* a1 = 0 (should be cause) +* a2 = pointer to struct sigcontext +* +* $25 and PC point to the signal handler, $29 points to the +* struct sigframe. +*/ + regs->active_tc.gpr[4 ] = sig; + regs->active_tc.gpr[5 ] =0 ; + regs->active_tc.gpr[6 ] = frame_addr + offsetof(struct sigframe , sf_sc); + regs->active_tc.gpr[29 ] = frame_addr; + regs->active_tc.gpr[31 ] = frame_addr + offsetof(struct sigframe , sf_code); +/* The original kernel code sets CP0_EPC to the handler +* since it returns to userland using eret +* we cannot do this here, and we must set PC directly */ + regs->active_tc.PC = regs->active_tc.gpr[25 ] = ka->_sa_handler; +unlock_user_struct (frame, frame_addr,1 ); +return ; + +give_sigsegv: +unlock_user_struct (frame, frame_addr,1 ); +force_sig (TARGET_SIGSEGV /*, current*/ ); +return ; +} + +long do_sigreturn (CPUState *regs) +{ +struct sigframe *frame; +abi_ulong frame_addr; +sigset_t blocked; +target_sigset_t target_set; +int i; + +#if defined(DEBUG_SIGNAL ) +fprintf (stderr,"do_sigreturn\n" ); +#endif + frame_addr = regs->active_tc.gpr[29 ]; +if (!lock_user_struct (VERIFY_READ , frame, frame_addr,1 )) + goto badframe; + +for (i =0 ; i <TARGET_NSIG_WORDS ; i++) { +if (__get_user (target_set.sig[i], &frame->sf_mask.sig[i])) + goto badframe; + } + +target_to_host_sigset_internal (&blocked, &target_set); +sigprocmask (SIG_SETMASK , &blocked,NULL ); + +if (restore_sigcontext (regs, &frame->sf_sc)) + goto badframe; + +#if 0 +/* +* Don't let your children do this ... +*/ + __asm__ __volatile__( +"move\t$29, %0\n\t" +"j\tsyscall_exit" + :/* no outputs */ + :"r" (®s)); +/* Unreached */ +#endif + + regs->active_tc.PC = regs->CP0_EPC; +/* I am not sure this is right, but it seems to work +* maybe a problem with nested signals ? */ + regs->CP0_EPC =0 ; +return -TARGET_QEMU_ESIGRETURN ; + +badframe: +force_sig (TARGET_SIGSEGV /*, current*/ ); +return 0 ; +} + +#else + +struct target_sigcontext { +uint64_t sc_regs[32 ]; +uint64_t sc_fpregs[32 ]; +uint64_t sc_mdhi; +uint64_t sc_hi1; +uint64_t sc_hi2; +uint64_t sc_hi3; +uint64_t sc_mdlo; +uint64_t sc_lo1; +uint64_t sc_lo2; +uint64_t sc_lo3; +uint64_t sc_pc; +uint32_t sc_fpc_csr; +uint32_t sc_used_math; +uint32_t sc_dsp; +uint32_t sc_reserved; +}; +static void setup_frame (int sig,struct target_sigaction *ka, +target_sigset_t *set,CPUState *env) +{ +fprintf (stderr,"setup_frame: not implemented\n" ); +} +long do_sigreturn (CPUState *env) +{ +fprintf (stderr,"do_sigreturn: not implemented\n" ); +return -TARGET_ENOSYS ; +} +#endif +struct sigframe {uint32_t sf_ass[4 ];/* argument save space for o32 */ uint32_t sf_code[2 ];/* signal trampoline */
@@ -2706,105 +2817,6 @@ get_sigframe(struct target_sigaction *ka, CPUState *regs, size_t frame_size)return (sp - frame_size) & ~7 ; } -/* compare linux/arch/mips/kernel/signal.c:setup_frame() */ -static void setup_frame (int sig,struct target_sigaction * ka, -target_sigset_t *set,CPUState *regs) -{ -struct sigframe *frame; -abi_ulong frame_addr; -int i; - - frame_addr =get_sigframe (ka, regs,sizeof (*frame)); -if (!lock_user_struct (VERIFY_WRITE , frame, frame_addr,0 )) - goto give_sigsegv; - -install_sigtramp (frame->sf_code, TARGET_NR_sigreturn); - -if (setup_sigcontext (regs, &frame->sf_sc)) - goto give_sigsegv; - -for (i =0 ; i <TARGET_NSIG_WORDS ; i++) { -if (__put_user (set->sig[i], &frame->sf_mask.sig[i])) - goto give_sigsegv; - } - -/* -* Arguments to signal handler: -* -* a0 = signal number -* a1 = 0 (should be cause) -* a2 = pointer to struct sigcontext -* -* $25 and PC point to the signal handler, $29 points to the -* struct sigframe. -*/ - regs->active_tc.gpr[4 ] = sig; - regs->active_tc.gpr[5 ] =0 ; - regs->active_tc.gpr[6 ] = frame_addr + offsetof(struct sigframe , sf_sc); - regs->active_tc.gpr[29 ] = frame_addr; - regs->active_tc.gpr[31 ] = frame_addr + offsetof(struct sigframe , sf_code); -/* The original kernel code sets CP0_EPC to the handler -* since it returns to userland using eret -* we cannot do this here, and we must set PC directly */ - regs->active_tc.PC = regs->active_tc.gpr[25 ] = ka->_sa_handler; -unlock_user_struct (frame, frame_addr,1 ); -return ; - -give_sigsegv: -unlock_user_struct (frame, frame_addr,1 ); -force_sig (TARGET_SIGSEGV /*, current*/ ); -return ; -} - -long do_sigreturn (CPUState *regs) -{ -struct sigframe *frame; -abi_ulong frame_addr; -sigset_t blocked; -target_sigset_t target_set; -int i; - -#if defined(DEBUG_SIGNAL ) -fprintf (stderr,"do_sigreturn\n" ); -#endif - frame_addr = regs->active_tc.gpr[29 ]; -if (!lock_user_struct (VERIFY_READ , frame, frame_addr,1 )) - goto badframe; - -for (i =0 ; i <TARGET_NSIG_WORDS ; i++) { -if (__get_user (target_set.sig[i], &frame->sf_mask.sig[i])) - goto badframe; - } - -target_to_host_sigset_internal (&blocked, &target_set); -sigprocmask (SIG_SETMASK , &blocked,NULL ); - -if (restore_sigcontext (regs, &frame->sf_sc)) - goto badframe; - -#if 0 -/* -* Don't let your children do this ... -*/ - __asm__ __volatile__( -"move\t$29, %0\n\t" -"j\tsyscall_exit" - :/* no outputs */ - :"r" (®s)); -/* Unreached */ -#endif - - regs->active_tc.PC = regs->CP0_EPC; -/* I am not sure this is right, but it seems to work -* maybe a problem with nested signals ? */ - regs->CP0_EPC =0 ; -return -TARGET_QEMU_ESIGRETURN ; - -badframe: -force_sig (TARGET_SIGSEGV /*, current*/ ); -return 0 ; -} -static void setup_rt_frame (int sig,struct target_sigaction *ka,target_siginfo_t *info,target_sigset_t *set,CPUState *env)
Signature · 1 line
1.7.3.4
Ping?
Quoted 42 lines · On Tue, Jan 3, 2012 at 9:54 AM, <[email protected]> wrote:
From: Khansa Butt <[email protected]>
[sorry version missed in previous set]
This is the team work of Ehsan-ul-Haq, Abdul Qadeer, Abdul Waheed, Khansa Butt from HPCN Lab KICS UET Lahore.
In previous patch set we were including Cavium specific instructions along with Cavium specifc registers in UME. Because of these register fields we had to bump the cpu version up but I noticed thatcpu_save()andcpu_load()are not called in UME so we decided to postpone Octeon specific changes ( registers and instructions) and will include them in our SME Patches.
MIPS64el is included in Andreas Färber patch series so I have not include it in our patches (we are working on mips64el but mips64el binary is needed)
v3-->v4
1)Drop Cavium specific instructions and save/load of Cavium's registers
2) signal handling of mips64 has been changed according to Andreas Färber and Richard Henderson v2-->v3
1)SEQI and SEQ related changes
2)Cavium specific change inset_thread_areasyscall has been removed
as it corresponds to modified libc and kernel.
v1-->v2
1)Fix issues related to coding style, typos and misleading comments
2)Addition of helper functions for sum of Cavium specific instructions
v1 contains
1)MIPS64 user mode emulation in QEMU
2) Octeon CPU definition + Cavium Specific Instructions
linux-user: Add support for MIPS64
target-mips: Enable 64 bit user mode
target-mips: Fix FPU reset
linux-user: Implement signal handling for mips64configure | 1 + default-configs/mips64-linux-user.mak | 1 +
linux-user/main.c | 21 +++-linux-user/mips64/syscall.h| 2 +linux-user/signal.c| 272 +++++++++++++++++----------------target-mips/translate.c| 4 +6 files changed, 169 insertions(+), 132 deletions(-)create mode 100644 default-configs/mips64-linux-user.mak
--
1.7.3.4