Skip to content

Commit

Permalink
{linux,bsd}-user: Introduce get_task_state()
Browse files Browse the repository at this point in the history
A CPU's TaskState is stored in the CPUState's void *opaque field,
accessing which is somewhat awkward due to having to use a cast.
Introduce a wrapper and use it everywhere.

Suggested-by: Alex Bennée <alex.bennee@linaro.org>
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
Reviewed-by: Warner Losh <imp@bsdimp.com>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Message-Id: <20240219141628.246823-3-iii@linux.ibm.com>
Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
Message-Id: <20240305121005.3528075-4-alex.bennee@linaro.org>
  • Loading branch information
iii-i authored and stsquad committed Mar 6, 2024
1 parent 1ea96f1 commit e4e5cb4
Show file tree
Hide file tree
Showing 25 changed files with 85 additions and 75 deletions.
2 changes: 1 addition & 1 deletion bsd-user/bsd-file.h
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ static abi_long do_bsd_readlink(CPUArchState *env, abi_long arg1,
}
if (strcmp(p1, "/proc/curproc/file") == 0) {
CPUState *cpu = env_cpu(env);
TaskState *ts = (TaskState *)cpu->opaque;
TaskState *ts = get_task_state(cpu);
strncpy(p2, ts->bprm->fullpath, arg3);
ret = MIN((abi_long)strlen(ts->bprm->fullpath), arg3);
} else {
Expand Down
5 changes: 5 additions & 0 deletions bsd-user/qemu.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ typedef struct TaskState {
struct target_sigaltstack sigaltstack_used;
} __attribute__((aligned(16))) TaskState;

static inline TaskState *get_task_state(CPUState *cs)
{
return cs->opaque;
}

void stop_all_tasks(void);
extern const char *interp_prefix;
extern const char *qemu_uname_release;
Expand Down
20 changes: 10 additions & 10 deletions bsd-user/signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info)

int block_signals(void)
{
TaskState *ts = (TaskState *)thread_cpu->opaque;
TaskState *ts = get_task_state(thread_cpu);
sigset_t set;

/*
Expand Down Expand Up @@ -359,7 +359,7 @@ void dump_core_and_abort(int target_sig)
{
CPUState *cpu = thread_cpu;
CPUArchState *env = cpu_env(cpu);
TaskState *ts = cpu->opaque;
TaskState *ts = get_task_state(cpu);
int core_dumped = 0;
int host_sig;
struct sigaction act;
Expand Down Expand Up @@ -421,7 +421,7 @@ void queue_signal(CPUArchState *env, int sig, int si_type,
target_siginfo_t *info)
{
CPUState *cpu = env_cpu(env);
TaskState *ts = cpu->opaque;
TaskState *ts = get_task_state(cpu);

trace_user_queue_signal(env, sig);

Expand Down Expand Up @@ -476,7 +476,7 @@ void force_sig_fault(int sig, int code, abi_ulong addr)
static void host_signal_handler(int host_sig, siginfo_t *info, void *puc)
{
CPUState *cpu = thread_cpu;
TaskState *ts = cpu->opaque;
TaskState *ts = get_task_state(cpu);
target_siginfo_t tinfo;
ucontext_t *uc = puc;
struct emulated_sigtable *k;
Expand Down Expand Up @@ -585,7 +585,7 @@ static void host_signal_handler(int host_sig, siginfo_t *info, void *puc)
/* compare to kern/kern_sig.c sys_sigaltstack() and kern_sigaltstack() */
abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp)
{
TaskState *ts = (TaskState *)thread_cpu->opaque;
TaskState *ts = get_task_state(thread_cpu);
int ret;
target_stack_t oss;

Expand Down Expand Up @@ -714,7 +714,7 @@ int do_sigaction(int sig, const struct target_sigaction *act,
static inline abi_ulong get_sigframe(struct target_sigaction *ka,
CPUArchState *env, size_t frame_size)
{
TaskState *ts = (TaskState *)thread_cpu->opaque;
TaskState *ts = get_task_state(thread_cpu);
abi_ulong sp;

/* Use default user stack */
Expand Down Expand Up @@ -789,7 +789,7 @@ static int reset_signal_mask(target_ucontext_t *ucontext)
int i;
sigset_t blocked;
target_sigset_t target_set;
TaskState *ts = (TaskState *)thread_cpu->opaque;
TaskState *ts = get_task_state(thread_cpu);

for (i = 0; i < TARGET_NSIG_WORDS; i++) {
__get_user(target_set.__bits[i], &ucontext->uc_sigmask.__bits[i]);
Expand Down Expand Up @@ -839,7 +839,7 @@ long do_sigreturn(CPUArchState *env, abi_ulong addr)

void signal_init(void)
{
TaskState *ts = (TaskState *)thread_cpu->opaque;
TaskState *ts = get_task_state(thread_cpu);
struct sigaction act;
struct sigaction oact;
int i;
Expand Down Expand Up @@ -878,7 +878,7 @@ static void handle_pending_signal(CPUArchState *env, int sig,
struct emulated_sigtable *k)
{
CPUState *cpu = env_cpu(env);
TaskState *ts = cpu->opaque;
TaskState *ts = get_task_state(cpu);
struct target_sigaction *sa;
int code;
sigset_t set;
Expand Down Expand Up @@ -967,7 +967,7 @@ void process_pending_signals(CPUArchState *env)
int sig;
sigset_t *blocked_set, set;
struct emulated_sigtable *k;
TaskState *ts = cpu->opaque;
TaskState *ts = get_task_state(cpu);

while (qatomic_read(&ts->signal_pending)) {
sigfillset(&set);
Expand Down
4 changes: 2 additions & 2 deletions gdbstub/user-target.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ int gdb_target_signal_to_gdb(int sig)

int gdb_get_cpu_index(CPUState *cpu)
{
TaskState *ts = (TaskState *) cpu->opaque;
TaskState *ts = get_task_state(cpu);
return ts ? ts->ts_tid : -1;
}

Expand Down Expand Up @@ -399,7 +399,7 @@ void gdb_handle_query_xfer_exec_file(GArray *params, void *user_ctx)
return;
}

TaskState *ts = cpu->opaque;
TaskState *ts = get_task_state(cpu);
if (!ts || !ts->bprm || !ts->bprm->filename) {
gdb_put_packet("E00");
return;
Expand Down
2 changes: 1 addition & 1 deletion include/user/safe-syscall.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ extern char safe_syscall_start[];
extern char safe_syscall_end[];

#define safe_syscall(...) \
safe_syscall_base(&((TaskState *)thread_cpu->opaque)->signal_pending, \
safe_syscall_base(&get_task_state(thread_cpu)->signal_pending, \
__VA_ARGS__)

#endif
2 changes: 1 addition & 1 deletion linux-user/aarch64/cpu_loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ void target_cpu_copy_regs(CPUArchState *env, struct target_pt_regs *regs)
{
ARMCPU *cpu = env_archcpu(env);
CPUState *cs = env_cpu(env);
TaskState *ts = cs->opaque;
TaskState *ts = get_task_state(cs);
struct image_info *info = ts->info;
int i;

Expand Down
4 changes: 2 additions & 2 deletions linux-user/arm/cpu_loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ static bool insn_is_linux_bkpt(uint32_t opcode, bool is_thumb)

static bool emulate_arm_fpa11(CPUARMState *env, uint32_t opcode)
{
TaskState *ts = env_cpu(env)->opaque;
TaskState *ts = get_task_state(env_cpu(env));
int rc = EmulateAll(opcode, &ts->fpa, env);
int raise, enabled;

Expand Down Expand Up @@ -514,7 +514,7 @@ void cpu_loop(CPUARMState *env)
void target_cpu_copy_regs(CPUArchState *env, struct target_pt_regs *regs)
{
CPUState *cpu = env_cpu(env);
TaskState *ts = cpu->opaque;
TaskState *ts = get_task_state(cpu);
struct image_info *info = ts->info;
int i;

Expand Down
2 changes: 1 addition & 1 deletion linux-user/arm/signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ setup_return(CPUARMState *env, struct target_sigaction *ka, int usig,
abi_ulong handler = 0;
abi_ulong handler_fdpic_GOT = 0;
abi_ulong retcode;
bool is_fdpic = info_is_fdpic(((TaskState *)thread_cpu->opaque)->info);
bool is_fdpic = info_is_fdpic(get_task_state(thread_cpu)->info);
bool is_rt = ka->sa_flags & TARGET_SA_SIGINFO;
bool thumb;

Expand Down
2 changes: 1 addition & 1 deletion linux-user/cris/cpu_loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void cpu_loop(CPUCRISState *env)
void target_cpu_copy_regs(CPUArchState *env, struct target_pt_regs *regs)
{
CPUState *cpu = env_cpu(env);
TaskState *ts = cpu->opaque;
TaskState *ts = get_task_state(cpu);
struct image_info *info = ts->info;

env->regs[0] = regs->r0;
Expand Down
2 changes: 1 addition & 1 deletion linux-user/elfload.c
Original file line number Diff line number Diff line change
Expand Up @@ -4404,7 +4404,7 @@ static int wmr_write_region(void *opaque, target_ulong start,
static int elf_core_dump(int signr, const CPUArchState *env)
{
const CPUState *cpu = env_cpu((CPUArchState *)env);
const TaskState *ts = (const TaskState *)cpu->opaque;
const TaskState *ts = (const TaskState *)get_task_state((CPUState *)cpu);
struct rlimit dumpsize;
CountAndSizeRegions css;
off_t offset, note_offset, data_offset;
Expand Down
2 changes: 1 addition & 1 deletion linux-user/hppa/signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ void setup_rt_frame(int sig, struct target_sigaction *ka,
abi_ulong frame_addr, sp, haddr;
struct target_rt_sigframe *frame;
int i;
TaskState *ts = (TaskState *)thread_cpu->opaque;
TaskState *ts = get_task_state(thread_cpu);

sp = get_sp_from_cpustate(env);
if ((ka->sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp)) {
Expand Down
2 changes: 1 addition & 1 deletion linux-user/linuxload.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ static int prepare_binprm(struct linux_binprm *bprm)
abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
abi_ulong stringp, int push_ptr)
{
TaskState *ts = (TaskState *)thread_cpu->opaque;
TaskState *ts = get_task_state(thread_cpu);
int n = sizeof(abi_ulong);
abi_ulong envp;
abi_ulong argv;
Expand Down
2 changes: 1 addition & 1 deletion linux-user/m68k/cpu_loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ void cpu_loop(CPUM68KState *env)
void target_cpu_copy_regs(CPUArchState *env, struct target_pt_regs *regs)
{
CPUState *cpu = env_cpu(env);
TaskState *ts = cpu->opaque;
TaskState *ts = get_task_state(cpu);
struct image_info *info = ts->info;

env->pc = regs->pc;
Expand Down
2 changes: 1 addition & 1 deletion linux-user/m68k/target_cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ static inline void cpu_clone_regs_parent(CPUM68KState *env, unsigned flags)
static inline void cpu_set_tls(CPUM68KState *env, target_ulong newtls)
{
CPUState *cs = env_cpu(env);
TaskState *ts = cs->opaque;
TaskState *ts = get_task_state(cs);

ts->tp_value = newtls;
}
Expand Down
2 changes: 1 addition & 1 deletion linux-user/mips/cpu_loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ void cpu_loop(CPUMIPSState *env)
void target_cpu_copy_regs(CPUArchState *env, struct target_pt_regs *regs)
{
CPUState *cpu = env_cpu(env);
TaskState *ts = cpu->opaque;
TaskState *ts = get_task_state(cpu);
struct image_info *info = ts->info;
int i;

Expand Down
4 changes: 2 additions & 2 deletions linux-user/ppc/signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ void setup_rt_frame(int sig, struct target_sigaction *ka,
int i, err = 0;
#if defined(TARGET_PPC64)
struct target_sigcontext *sc = 0;
struct image_info *image = ((TaskState *)thread_cpu->opaque)->info;
struct image_info *image = get_task_state(thread_cpu)->info;
#endif

rt_sf_addr = get_sigframe(ka, env, sizeof(*rt_sf));
Expand Down Expand Up @@ -673,7 +673,7 @@ abi_long do_swapcontext(CPUArchState *env, abi_ulong uold_ctx,
}

if (uold_ctx) {
TaskState *ts = (TaskState *)thread_cpu->opaque;
TaskState *ts = get_task_state(thread_cpu);

if (!lock_user_struct(VERIFY_WRITE, uctx, uold_ctx, 1)) {
return -TARGET_EFAULT;
Expand Down
5 changes: 5 additions & 0 deletions linux-user/qemu.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ typedef struct TaskState {
uint64_t start_boottime;
} TaskState;

static inline TaskState *get_task_state(CPUState *cs)
{
return cs->opaque;
}

abi_long do_brk(abi_ulong new_brk);
int do_guest_openat(CPUArchState *cpu_env, int dirfd, const char *pathname,
int flags, mode_t mode, bool safe);
Expand Down
2 changes: 1 addition & 1 deletion linux-user/riscv/cpu_loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ void cpu_loop(CPURISCVState *env)
void target_cpu_copy_regs(CPUArchState *env, struct target_pt_regs *regs)
{
CPUState *cpu = env_cpu(env);
TaskState *ts = cpu->opaque;
TaskState *ts = get_task_state(cpu);
struct image_info *info = ts->info;

env->pc = regs->sepc;
Expand Down
2 changes: 1 addition & 1 deletion linux-user/signal-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ int process_sigsuspend_mask(sigset_t **pset, target_ulong sigset,
static inline void finish_sigsuspend_mask(int ret)
{
if (ret != -QEMU_ERESTARTSYS) {
TaskState *ts = (TaskState *)thread_cpu->opaque;
TaskState *ts = get_task_state(thread_cpu);
ts->in_sigsuspend = 1;
}
}
Expand Down

0 comments on commit e4e5cb4

Please sign in to comment.