qemu/linux-user/sparc/signal.c
<<
>>
Prefs
   1/*
   2 *  Emulation of Linux signals
   3 *
   4 *  Copyright (c) 2003 Fabrice Bellard
   5 *
   6 *  This program is free software; you can redistribute it and/or modify
   7 *  it under the terms of the GNU General Public License as published by
   8 *  the Free Software Foundation; either version 2 of the License, or
   9 *  (at your option) any later version.
  10 *
  11 *  This program is distributed in the hope that it will be useful,
  12 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14 *  GNU General Public License for more details.
  15 *
  16 *  You should have received a copy of the GNU General Public License
  17 *  along with this program; if not, see <http://www.gnu.org/licenses/>.
  18 */
  19#include "qemu/osdep.h"
  20#include "qemu.h"
  21#include "signal-common.h"
  22#include "linux-user/trace.h"
  23
  24/* A Sparc register window */
  25struct target_reg_window {
  26    abi_ulong locals[8];
  27    abi_ulong ins[8];
  28};
  29
  30/* A Sparc stack frame. */
  31struct target_stackf {
  32    /*
  33     * Since qemu does not reference fp or callers_pc directly,
  34     * it's simpler to treat fp and callers_pc as elements of ins[],
  35     * and then bundle locals[] and ins[] into reg_window.
  36     */
  37    struct target_reg_window win;
  38    /*
  39     * Similarly, bundle structptr and xxargs into xargs[].
  40     * This portion of the struct is part of the function call abi,
  41     * and belongs to the callee for spilling argument registers.
  42     */
  43    abi_ulong xargs[8];
  44};
  45
  46struct target_siginfo_fpu {
  47#ifdef TARGET_SPARC64
  48    uint64_t si_double_regs[32];
  49    uint64_t si_fsr;
  50    uint64_t si_gsr;
  51    uint64_t si_fprs;
  52#else
  53    /* It is more convenient for qemu to move doubles, not singles. */
  54    uint64_t si_double_regs[16];
  55    uint32_t si_fsr;
  56    uint32_t si_fpqdepth;
  57    struct {
  58        uint32_t insn_addr;
  59        uint32_t insn;
  60    } si_fpqueue [16];
  61#endif
  62};
  63
  64#ifdef TARGET_ARCH_HAS_SETUP_FRAME
  65struct target_signal_frame {
  66    struct target_stackf ss;
  67    struct target_pt_regs regs;
  68    uint32_t si_mask;
  69    abi_ulong fpu_save;
  70    uint32_t insns[2] QEMU_ALIGNED(8);
  71    abi_ulong extramask[TARGET_NSIG_WORDS - 1];
  72    abi_ulong extra_size; /* Should be 0 */
  73    abi_ulong rwin_save;
  74};
  75#endif
  76
  77struct target_rt_signal_frame {
  78    struct target_stackf ss;
  79    target_siginfo_t info;
  80    struct target_pt_regs regs;
  81#if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
  82    abi_ulong fpu_save;
  83    target_stack_t stack;
  84    target_sigset_t mask;
  85#else
  86    target_sigset_t mask;
  87    abi_ulong fpu_save;
  88    uint32_t insns[2];
  89    target_stack_t stack;
  90    abi_ulong extra_size; /* Should be 0 */
  91#endif
  92    abi_ulong rwin_save;
  93};
  94
  95static abi_ulong get_sigframe(struct target_sigaction *sa,
  96                              CPUSPARCState *env,
  97                              size_t framesize)
  98{
  99    abi_ulong sp = get_sp_from_cpustate(env);
 100
 101    /*
 102     * If we are on the alternate signal stack and would overflow it, don't.
 103     * Return an always-bogus address instead so we will die with SIGSEGV.
 104     */
 105    if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize))) {
 106        return -1;
 107    }
 108
 109    /* This is the X/Open sanctioned signal stack switching.  */
 110    sp = target_sigsp(sp, sa) - framesize;
 111
 112    /*
 113     * Always align the stack frame.  This handles two cases.  First,
 114     * sigaltstack need not be mindful of platform specific stack
 115     * alignment.  Second, if we took this signal because the stack
 116     * is not aligned properly, we'd like to take the signal cleanly
 117     * and report that.
 118     */
 119    sp &= ~15UL;
 120
 121    return sp;
 122}
 123
 124static void save_pt_regs(struct target_pt_regs *regs, CPUSPARCState *env)
 125{
 126    int i;
 127
 128#if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
 129    __put_user(sparc64_tstate(env), &regs->tstate);
 130    /* TODO: magic should contain PT_REG_MAGIC + %tt. */
 131    __put_user(0, &regs->magic);
 132#else
 133    __put_user(cpu_get_psr(env), &regs->psr);
 134#endif
 135
 136    __put_user(env->pc, &regs->pc);
 137    __put_user(env->npc, &regs->npc);
 138    __put_user(env->y, &regs->y);
 139
 140    for (i = 0; i < 8; i++) {
 141        __put_user(env->gregs[i], &regs->u_regs[i]);
 142    }
 143    for (i = 0; i < 8; i++) {
 144        __put_user(env->regwptr[WREG_O0 + i], &regs->u_regs[i + 8]);
 145    }
 146}
 147
 148static void restore_pt_regs(struct target_pt_regs *regs, CPUSPARCState *env)
 149{
 150    int i;
 151
 152#if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
 153    /* User can only change condition codes and %asi in %tstate. */
 154    uint64_t tstate;
 155    __get_user(tstate, &regs->tstate);
 156    cpu_put_ccr(env, tstate >> 32);
 157    env->asi = extract64(tstate, 24, 8);
 158#else
 159    /*
 160     * User can only change condition codes and FPU enabling in %psr.
 161     * But don't bother with FPU enabling, since a real kernel would
 162     * just re-enable the FPU upon the next fpu trap.
 163     */
 164    uint32_t psr;
 165    __get_user(psr, &regs->psr);
 166    env->psr = (psr & PSR_ICC) | (env->psr & ~PSR_ICC);
 167#endif
 168
 169    /* Note that pc and npc are handled in the caller. */
 170
 171    __get_user(env->y, &regs->y);
 172
 173    for (i = 0; i < 8; i++) {
 174        __get_user(env->gregs[i], &regs->u_regs[i]);
 175    }
 176    for (i = 0; i < 8; i++) {
 177        __get_user(env->regwptr[WREG_O0 + i], &regs->u_regs[i + 8]);
 178    }
 179}
 180
 181static void save_reg_win(struct target_reg_window *win, CPUSPARCState *env)
 182{
 183    int i;
 184
 185    for (i = 0; i < 8; i++) {
 186        __put_user(env->regwptr[i + WREG_L0], &win->locals[i]);
 187    }
 188    for (i = 0; i < 8; i++) {
 189        __put_user(env->regwptr[i + WREG_I0], &win->ins[i]);
 190    }
 191}
 192
 193static void save_fpu(struct target_siginfo_fpu *fpu, CPUSPARCState *env)
 194{
 195    int i;
 196
 197#ifdef TARGET_SPARC64
 198    for (i = 0; i < 32; ++i) {
 199        __put_user(env->fpr[i].ll, &fpu->si_double_regs[i]);
 200    }
 201    __put_user(env->fsr, &fpu->si_fsr);
 202    __put_user(env->gsr, &fpu->si_gsr);
 203    __put_user(env->fprs, &fpu->si_fprs);
 204#else
 205    for (i = 0; i < 16; ++i) {
 206        __put_user(env->fpr[i].ll, &fpu->si_double_regs[i]);
 207    }
 208    __put_user(env->fsr, &fpu->si_fsr);
 209    __put_user(0, &fpu->si_fpqdepth);
 210#endif
 211}
 212
 213static void restore_fpu(struct target_siginfo_fpu *fpu, CPUSPARCState *env)
 214{
 215    int i;
 216
 217#ifdef TARGET_SPARC64
 218    uint64_t fprs;
 219    __get_user(fprs, &fpu->si_fprs);
 220
 221    /* In case the user mucks about with FPRS, restore as directed. */
 222    if (fprs & FPRS_DL) {
 223        for (i = 0; i < 16; ++i) {
 224            __get_user(env->fpr[i].ll, &fpu->si_double_regs[i]);
 225        }
 226    }
 227    if (fprs & FPRS_DU) {
 228        for (i = 16; i < 32; ++i) {
 229            __get_user(env->fpr[i].ll, &fpu->si_double_regs[i]);
 230        }
 231    }
 232    __get_user(env->fsr, &fpu->si_fsr);
 233    __get_user(env->gsr, &fpu->si_gsr);
 234    env->fprs |= fprs;
 235#else
 236    for (i = 0; i < 16; ++i) {
 237        __get_user(env->fpr[i].ll, &fpu->si_double_regs[i]);
 238    }
 239    __get_user(env->fsr, &fpu->si_fsr);
 240#endif
 241}
 242
 243#ifdef TARGET_ARCH_HAS_SETUP_FRAME
 244void setup_frame(int sig, struct target_sigaction *ka,
 245                 target_sigset_t *set, CPUSPARCState *env)
 246{
 247    abi_ulong sf_addr;
 248    struct target_signal_frame *sf;
 249    size_t sf_size = sizeof(*sf) + sizeof(struct target_siginfo_fpu);
 250    int i;
 251
 252    sf_addr = get_sigframe(ka, env, sf_size);
 253    trace_user_setup_frame(env, sf_addr);
 254
 255    sf = lock_user(VERIFY_WRITE, sf_addr, sf_size, 0);
 256    if (!sf) {
 257        force_sigsegv(sig);
 258        return;
 259    }
 260
 261    /* 2. Save the current process state */
 262    save_pt_regs(&sf->regs, env);
 263    __put_user(0, &sf->extra_size);
 264
 265    save_fpu((struct target_siginfo_fpu *)(sf + 1), env);
 266    __put_user(sf_addr + sizeof(*sf), &sf->fpu_save);
 267
 268    __put_user(0, &sf->rwin_save);  /* TODO: save_rwin_state */
 269
 270    __put_user(set->sig[0], &sf->si_mask);
 271    for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
 272        __put_user(set->sig[i + 1], &sf->extramask[i]);
 273    }
 274
 275    save_reg_win(&sf->ss.win, env);
 276
 277    /* 3. signal handler back-trampoline and parameters */
 278    env->regwptr[WREG_SP] = sf_addr;
 279    env->regwptr[WREG_O0] = sig;
 280    env->regwptr[WREG_O1] = sf_addr +
 281            offsetof(struct target_signal_frame, regs);
 282    env->regwptr[WREG_O2] = sf_addr +
 283            offsetof(struct target_signal_frame, regs);
 284
 285    /* 4. signal handler */
 286    env->pc = ka->_sa_handler;
 287    env->npc = env->pc + 4;
 288
 289    /* 5. return to kernel instructions */
 290    if (ka->ka_restorer) {
 291        env->regwptr[WREG_O7] = ka->ka_restorer;
 292    } else {
 293        env->regwptr[WREG_O7] = sf_addr +
 294                offsetof(struct target_signal_frame, insns) - 2 * 4;
 295
 296        /* mov __NR_sigreturn, %g1 */
 297        __put_user(0x821020d8u, &sf->insns[0]);
 298        /* t 0x10 */
 299        __put_user(0x91d02010u, &sf->insns[1]);
 300    }
 301    unlock_user(sf, sf_addr, sf_size);
 302}
 303#endif /* TARGET_ARCH_HAS_SETUP_FRAME */
 304
 305void setup_rt_frame(int sig, struct target_sigaction *ka,
 306                    target_siginfo_t *info,
 307                    target_sigset_t *set, CPUSPARCState *env)
 308{
 309    abi_ulong sf_addr;
 310    struct target_rt_signal_frame *sf;
 311    size_t sf_size = sizeof(*sf) + sizeof(struct target_siginfo_fpu);
 312
 313    sf_addr = get_sigframe(ka, env, sf_size);
 314    trace_user_setup_rt_frame(env, sf_addr);
 315
 316    sf = lock_user(VERIFY_WRITE, sf_addr, sf_size, 0);
 317    if (!sf) {
 318        force_sigsegv(sig);
 319        return;
 320    }
 321
 322    /* 2. Save the current process state */
 323    save_reg_win(&sf->ss.win, env);
 324    save_pt_regs(&sf->regs, env);
 325
 326    save_fpu((struct target_siginfo_fpu *)(sf + 1), env);
 327    __put_user(sf_addr + sizeof(*sf), &sf->fpu_save);
 328
 329    __put_user(0, &sf->rwin_save);  /* TODO: save_rwin_state */
 330
 331    tswap_siginfo(&sf->info, info);
 332    tswap_sigset(&sf->mask, set);
 333    target_save_altstack(&sf->stack, env);
 334
 335#ifdef TARGET_ABI32
 336    __put_user(0, &sf->extra_size);
 337#endif
 338
 339    /* 3. signal handler back-trampoline and parameters */
 340    env->regwptr[WREG_SP] = sf_addr - TARGET_STACK_BIAS;
 341    env->regwptr[WREG_O0] = sig;
 342    env->regwptr[WREG_O1] =
 343        sf_addr + offsetof(struct target_rt_signal_frame, info);
 344#ifdef TARGET_ABI32
 345    env->regwptr[WREG_O2] =
 346        sf_addr + offsetof(struct target_rt_signal_frame, regs);
 347#else
 348    env->regwptr[WREG_O2] = env->regwptr[WREG_O1];
 349#endif
 350
 351    /* 4. signal handler */
 352    env->pc = ka->_sa_handler;
 353    env->npc = env->pc + 4;
 354
 355    /* 5. return to kernel instructions */
 356#ifdef TARGET_ABI32
 357    if (ka->ka_restorer) {
 358        env->regwptr[WREG_O7] = ka->ka_restorer;
 359    } else {
 360        env->regwptr[WREG_O7] =
 361            sf_addr + offsetof(struct target_rt_signal_frame, insns) - 2 * 4;
 362
 363        /* mov __NR_rt_sigreturn, %g1 */
 364        __put_user(0x82102065u, &sf->insns[0]);
 365        /* t 0x10 */
 366        __put_user(0x91d02010u, &sf->insns[1]);
 367    }
 368#else
 369    env->regwptr[WREG_O7] = ka->ka_restorer;
 370#endif
 371
 372    unlock_user(sf, sf_addr, sf_size);
 373}
 374
 375long do_sigreturn(CPUSPARCState *env)
 376{
 377#ifdef TARGET_ARCH_HAS_SETUP_FRAME
 378    abi_ulong sf_addr;
 379    struct target_signal_frame *sf = NULL;
 380    abi_ulong pc, npc, ptr;
 381    target_sigset_t set;
 382    sigset_t host_set;
 383    int i;
 384
 385    sf_addr = env->regwptr[WREG_SP];
 386    trace_user_do_sigreturn(env, sf_addr);
 387
 388    /* 1. Make sure we are not getting garbage from the user */
 389    if ((sf_addr & 15) || !lock_user_struct(VERIFY_READ, sf, sf_addr, 1)) {
 390        goto segv_and_exit;
 391    }
 392
 393    /* Make sure stack pointer is aligned.  */
 394    __get_user(ptr, &sf->regs.u_regs[14]);
 395    if (ptr & 7) {
 396        goto segv_and_exit;
 397    }
 398
 399    /* Make sure instruction pointers are aligned.  */
 400    __get_user(pc, &sf->regs.pc);
 401    __get_user(npc, &sf->regs.npc);
 402    if ((pc | npc) & 3) {
 403        goto segv_and_exit;
 404    }
 405
 406    /* 2. Restore the state */
 407    restore_pt_regs(&sf->regs, env);
 408    env->pc = pc;
 409    env->npc = npc;
 410
 411    __get_user(ptr, &sf->fpu_save);
 412    if (ptr) {
 413        struct target_siginfo_fpu *fpu;
 414        if ((ptr & 3) || !lock_user_struct(VERIFY_READ, fpu, ptr, 1)) {
 415            goto segv_and_exit;
 416        }
 417        restore_fpu(fpu, env);
 418        unlock_user_struct(fpu, ptr, 0);
 419    }
 420
 421    __get_user(ptr, &sf->rwin_save);
 422    if (ptr) {
 423        goto segv_and_exit;  /* TODO: restore_rwin */
 424    }
 425
 426    __get_user(set.sig[0], &sf->si_mask);
 427    for (i = 1; i < TARGET_NSIG_WORDS; i++) {
 428        __get_user(set.sig[i], &sf->extramask[i - 1]);
 429    }
 430
 431    target_to_host_sigset_internal(&host_set, &set);
 432    set_sigmask(&host_set);
 433
 434    unlock_user_struct(sf, sf_addr, 0);
 435    return -TARGET_QEMU_ESIGRETURN;
 436
 437 segv_and_exit:
 438    unlock_user_struct(sf, sf_addr, 0);
 439    force_sig(TARGET_SIGSEGV);
 440    return -TARGET_QEMU_ESIGRETURN;
 441#else
 442    return -TARGET_ENOSYS;
 443#endif
 444}
 445
 446long do_rt_sigreturn(CPUSPARCState *env)
 447{
 448    abi_ulong sf_addr, tpc, tnpc, ptr;
 449    struct target_rt_signal_frame *sf = NULL;
 450    sigset_t set;
 451
 452    sf_addr = get_sp_from_cpustate(env);
 453    trace_user_do_rt_sigreturn(env, sf_addr);
 454
 455    /* 1. Make sure we are not getting garbage from the user */
 456    if ((sf_addr & 15) || !lock_user_struct(VERIFY_READ, sf, sf_addr, 1)) {
 457        goto segv_and_exit;
 458    }
 459
 460    /* Validate SP alignment.  */
 461    __get_user(ptr, &sf->regs.u_regs[8 + WREG_SP]);
 462    if ((ptr + TARGET_STACK_BIAS) & 7) {
 463        goto segv_and_exit;
 464    }
 465
 466    /* Validate PC and NPC alignment.  */
 467    __get_user(tpc, &sf->regs.pc);
 468    __get_user(tnpc, &sf->regs.npc);
 469    if ((tpc | tnpc) & 3) {
 470        goto segv_and_exit;
 471    }
 472
 473    /* 2. Restore the state */
 474    restore_pt_regs(&sf->regs, env);
 475
 476    __get_user(ptr, &sf->fpu_save);
 477    if (ptr) {
 478        struct target_siginfo_fpu *fpu;
 479        if ((ptr & 7) || !lock_user_struct(VERIFY_READ, fpu, ptr, 1)) {
 480            goto segv_and_exit;
 481        }
 482        restore_fpu(fpu, env);
 483        unlock_user_struct(fpu, ptr, 0);
 484    }
 485
 486    __get_user(ptr, &sf->rwin_save);
 487    if (ptr) {
 488        goto segv_and_exit;  /* TODO: restore_rwin_state */
 489    }
 490
 491    target_restore_altstack(&sf->stack, env);
 492    target_to_host_sigset(&set, &sf->mask);
 493    set_sigmask(&set);
 494
 495    env->pc = tpc;
 496    env->npc = tnpc;
 497
 498    unlock_user_struct(sf, sf_addr, 0);
 499    return -TARGET_QEMU_ESIGRETURN;
 500
 501 segv_and_exit:
 502    unlock_user_struct(sf, sf_addr, 0);
 503    force_sig(TARGET_SIGSEGV);
 504    return -TARGET_QEMU_ESIGRETURN;
 505}
 506
 507#if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
 508#define SPARC_MC_TSTATE 0
 509#define SPARC_MC_PC 1
 510#define SPARC_MC_NPC 2
 511#define SPARC_MC_Y 3
 512#define SPARC_MC_G1 4
 513#define SPARC_MC_G2 5
 514#define SPARC_MC_G3 6
 515#define SPARC_MC_G4 7
 516#define SPARC_MC_G5 8
 517#define SPARC_MC_G6 9
 518#define SPARC_MC_G7 10
 519#define SPARC_MC_O0 11
 520#define SPARC_MC_O1 12
 521#define SPARC_MC_O2 13
 522#define SPARC_MC_O3 14
 523#define SPARC_MC_O4 15
 524#define SPARC_MC_O5 16
 525#define SPARC_MC_O6 17
 526#define SPARC_MC_O7 18
 527#define SPARC_MC_NGREG 19
 528
 529typedef abi_ulong target_mc_greg_t;
 530typedef target_mc_greg_t target_mc_gregset_t[SPARC_MC_NGREG];
 531
 532struct target_mc_fq {
 533    abi_ulong mcfq_addr;
 534    uint32_t mcfq_insn;
 535};
 536
 537/*
 538 * Note the manual 16-alignment; the kernel gets this because it
 539 * includes a "long double qregs[16]" in the mcpu_fregs union,
 540 * which we can't do.
 541 */
 542struct target_mc_fpu {
 543    union {
 544        uint32_t sregs[32];
 545        uint64_t dregs[32];
 546        //uint128_t qregs[16];
 547    } mcfpu_fregs;
 548    abi_ulong mcfpu_fsr;
 549    abi_ulong mcfpu_fprs;
 550    abi_ulong mcfpu_gsr;
 551    abi_ulong mcfpu_fq;
 552    unsigned char mcfpu_qcnt;
 553    unsigned char mcfpu_qentsz;
 554    unsigned char mcfpu_enab;
 555} __attribute__((aligned(16)));
 556typedef struct target_mc_fpu target_mc_fpu_t;
 557
 558typedef struct {
 559    target_mc_gregset_t mc_gregs;
 560    target_mc_greg_t mc_fp;
 561    target_mc_greg_t mc_i7;
 562    target_mc_fpu_t mc_fpregs;
 563} target_mcontext_t;
 564
 565struct target_ucontext {
 566    abi_ulong tuc_link;
 567    abi_ulong tuc_flags;
 568    target_sigset_t tuc_sigmask;
 569    target_mcontext_t tuc_mcontext;
 570};
 571
 572/* {set, get}context() needed for 64-bit SparcLinux userland. */
 573void sparc64_set_context(CPUSPARCState *env)
 574{
 575    abi_ulong ucp_addr;
 576    struct target_ucontext *ucp;
 577    target_mc_gregset_t *grp;
 578    target_mc_fpu_t *fpup;
 579    abi_ulong pc, npc, tstate;
 580    unsigned int i;
 581    unsigned char fenab;
 582
 583    ucp_addr = env->regwptr[WREG_O0];
 584    if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1)) {
 585        goto do_sigsegv;
 586    }
 587    grp  = &ucp->tuc_mcontext.mc_gregs;
 588    __get_user(pc, &((*grp)[SPARC_MC_PC]));
 589    __get_user(npc, &((*grp)[SPARC_MC_NPC]));
 590    if ((pc | npc) & 3) {
 591        goto do_sigsegv;
 592    }
 593    if (env->regwptr[WREG_O1]) {
 594        target_sigset_t target_set;
 595        sigset_t set;
 596
 597        if (TARGET_NSIG_WORDS == 1) {
 598            __get_user(target_set.sig[0], &ucp->tuc_sigmask.sig[0]);
 599        } else {
 600            abi_ulong *src, *dst;
 601            src = ucp->tuc_sigmask.sig;
 602            dst = target_set.sig;
 603            for (i = 0; i < TARGET_NSIG_WORDS; i++, dst++, src++) {
 604                __get_user(*dst, src);
 605            }
 606        }
 607        target_to_host_sigset_internal(&set, &target_set);
 608        set_sigmask(&set);
 609    }
 610    env->pc = pc;
 611    env->npc = npc;
 612    __get_user(env->y, &((*grp)[SPARC_MC_Y]));
 613    __get_user(tstate, &((*grp)[SPARC_MC_TSTATE]));
 614    /* Honour TSTATE_ASI, TSTATE_ICC and TSTATE_XCC only */
 615    env->asi = (tstate >> 24) & 0xff;
 616    cpu_put_ccr(env, (tstate >> 32) & 0xff);
 617    __get_user(env->gregs[1], (&(*grp)[SPARC_MC_G1]));
 618    __get_user(env->gregs[2], (&(*grp)[SPARC_MC_G2]));
 619    __get_user(env->gregs[3], (&(*grp)[SPARC_MC_G3]));
 620    __get_user(env->gregs[4], (&(*grp)[SPARC_MC_G4]));
 621    __get_user(env->gregs[5], (&(*grp)[SPARC_MC_G5]));
 622    __get_user(env->gregs[6], (&(*grp)[SPARC_MC_G6]));
 623    /* Skip g7 as that's the thread register in userspace */
 624
 625    /*
 626     * Note that unlike the kernel, we didn't need to mess with the
 627     * guest register window state to save it into a pt_regs to run
 628     * the kernel. So for us the guest's O regs are still in WREG_O*
 629     * (unlike the kernel which has put them in UREG_I* in a pt_regs)
 630     * and the fp and i7 are still in WREG_I6 and WREG_I7 and don't
 631     * need to be written back to userspace memory.
 632     */
 633    __get_user(env->regwptr[WREG_O0], (&(*grp)[SPARC_MC_O0]));
 634    __get_user(env->regwptr[WREG_O1], (&(*grp)[SPARC_MC_O1]));
 635    __get_user(env->regwptr[WREG_O2], (&(*grp)[SPARC_MC_O2]));
 636    __get_user(env->regwptr[WREG_O3], (&(*grp)[SPARC_MC_O3]));
 637    __get_user(env->regwptr[WREG_O4], (&(*grp)[SPARC_MC_O4]));
 638    __get_user(env->regwptr[WREG_O5], (&(*grp)[SPARC_MC_O5]));
 639    __get_user(env->regwptr[WREG_O6], (&(*grp)[SPARC_MC_O6]));
 640    __get_user(env->regwptr[WREG_O7], (&(*grp)[SPARC_MC_O7]));
 641
 642    __get_user(env->regwptr[WREG_FP], &(ucp->tuc_mcontext.mc_fp));
 643    __get_user(env->regwptr[WREG_I7], &(ucp->tuc_mcontext.mc_i7));
 644
 645    fpup = &ucp->tuc_mcontext.mc_fpregs;
 646
 647    __get_user(fenab, &(fpup->mcfpu_enab));
 648    if (fenab) {
 649        abi_ulong fprs;
 650
 651        /*
 652         * We use the FPRS from the guest only in deciding whether
 653         * to restore the upper, lower, or both banks of the FPU regs.
 654         * The kernel here writes the FPU register data into the
 655         * process's current_thread_info state and unconditionally
 656         * clears FPRS and TSTATE_PEF: this disables the FPU so that the
 657         * next FPU-disabled trap will copy the data out of
 658         * current_thread_info and into the real FPU registers.
 659         * QEMU doesn't need to handle lazy-FPU-state-restoring like that,
 660         * so we always load the data directly into the FPU registers
 661         * and leave FPRS and TSTATE_PEF alone (so the FPU stays enabled).
 662         * Note that because we (and the kernel) always write zeroes for
 663         * the fenab and fprs in sparc64_get_context() none of this code
 664         * will execute unless the guest manually constructed or changed
 665         * the context structure.
 666         */
 667        __get_user(fprs, &(fpup->mcfpu_fprs));
 668        if (fprs & FPRS_DL) {
 669            for (i = 0; i < 16; i++) {
 670                __get_user(env->fpr[i].ll, &(fpup->mcfpu_fregs.dregs[i]));
 671            }
 672        }
 673        if (fprs & FPRS_DU) {
 674            for (i = 16; i < 32; i++) {
 675                __get_user(env->fpr[i].ll, &(fpup->mcfpu_fregs.dregs[i]));
 676            }
 677        }
 678        __get_user(env->fsr, &(fpup->mcfpu_fsr));
 679        __get_user(env->gsr, &(fpup->mcfpu_gsr));
 680    }
 681    unlock_user_struct(ucp, ucp_addr, 0);
 682    return;
 683do_sigsegv:
 684    unlock_user_struct(ucp, ucp_addr, 0);
 685    force_sig(TARGET_SIGSEGV);
 686}
 687
 688void sparc64_get_context(CPUSPARCState *env)
 689{
 690    abi_ulong ucp_addr;
 691    struct target_ucontext *ucp;
 692    target_mc_gregset_t *grp;
 693    target_mcontext_t *mcp;
 694    int err;
 695    unsigned int i;
 696    target_sigset_t target_set;
 697    sigset_t set;
 698
 699    ucp_addr = env->regwptr[WREG_O0];
 700    if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0)) {
 701        goto do_sigsegv;
 702    }
 703
 704    memset(ucp, 0, sizeof(*ucp));
 705
 706    mcp = &ucp->tuc_mcontext;
 707    grp = &mcp->mc_gregs;
 708
 709    /* Skip over the trap instruction, first. */
 710    env->pc = env->npc;
 711    env->npc += 4;
 712
 713    /* If we're only reading the signal mask then do_sigprocmask()
 714     * is guaranteed not to fail, which is important because we don't
 715     * have any way to signal a failure or restart this operation since
 716     * this is not a normal syscall.
 717     */
 718    err = do_sigprocmask(0, NULL, &set);
 719    assert(err == 0);
 720    host_to_target_sigset_internal(&target_set, &set);
 721    if (TARGET_NSIG_WORDS == 1) {
 722        __put_user(target_set.sig[0],
 723                   (abi_ulong *)&ucp->tuc_sigmask);
 724    } else {
 725        abi_ulong *src, *dst;
 726        src = target_set.sig;
 727        dst = ucp->tuc_sigmask.sig;
 728        for (i = 0; i < TARGET_NSIG_WORDS; i++, dst++, src++) {
 729            __put_user(*src, dst);
 730        }
 731    }
 732
 733    __put_user(sparc64_tstate(env), &((*grp)[SPARC_MC_TSTATE]));
 734    __put_user(env->pc, &((*grp)[SPARC_MC_PC]));
 735    __put_user(env->npc, &((*grp)[SPARC_MC_NPC]));
 736    __put_user(env->y, &((*grp)[SPARC_MC_Y]));
 737    __put_user(env->gregs[1], &((*grp)[SPARC_MC_G1]));
 738    __put_user(env->gregs[2], &((*grp)[SPARC_MC_G2]));
 739    __put_user(env->gregs[3], &((*grp)[SPARC_MC_G3]));
 740    __put_user(env->gregs[4], &((*grp)[SPARC_MC_G4]));
 741    __put_user(env->gregs[5], &((*grp)[SPARC_MC_G5]));
 742    __put_user(env->gregs[6], &((*grp)[SPARC_MC_G6]));
 743    __put_user(env->gregs[7], &((*grp)[SPARC_MC_G7]));
 744
 745    /*
 746     * Note that unlike the kernel, we didn't need to mess with the
 747     * guest register window state to save it into a pt_regs to run
 748     * the kernel. So for us the guest's O regs are still in WREG_O*
 749     * (unlike the kernel which has put them in UREG_I* in a pt_regs)
 750     * and the fp and i7 are still in WREG_I6 and WREG_I7 and don't
 751     * need to be fished out of userspace memory.
 752     */
 753    __put_user(env->regwptr[WREG_O0], &((*grp)[SPARC_MC_O0]));
 754    __put_user(env->regwptr[WREG_O1], &((*grp)[SPARC_MC_O1]));
 755    __put_user(env->regwptr[WREG_O2], &((*grp)[SPARC_MC_O2]));
 756    __put_user(env->regwptr[WREG_O3], &((*grp)[SPARC_MC_O3]));
 757    __put_user(env->regwptr[WREG_O4], &((*grp)[SPARC_MC_O4]));
 758    __put_user(env->regwptr[WREG_O5], &((*grp)[SPARC_MC_O5]));
 759    __put_user(env->regwptr[WREG_O6], &((*grp)[SPARC_MC_O6]));
 760    __put_user(env->regwptr[WREG_O7], &((*grp)[SPARC_MC_O7]));
 761
 762    __put_user(env->regwptr[WREG_FP], &(mcp->mc_fp));
 763    __put_user(env->regwptr[WREG_I7], &(mcp->mc_i7));
 764
 765    /*
 766     * We don't write out the FPU state. This matches the kernel's
 767     * implementation (which has the code for doing this but
 768     * hidden behind an "if (fenab)" where fenab is always 0).
 769     */
 770
 771    unlock_user_struct(ucp, ucp_addr, 1);
 772    return;
 773do_sigsegv:
 774    unlock_user_struct(ucp, ucp_addr, 1);
 775    force_sig(TARGET_SIGSEGV);
 776}
 777#endif
 778