static void compute_stats(HTTPContext *c)

{

    HTTPContext *c1;

    FFStream *stream;

    char *p;

    time_t ti;

    int i, len;

    ByteIOContext pb1, *pb = &pb1;

    if (url_open_dyn_buf(pb) < 0) {

        /* XXX: return an error ? */

        c->buffer_ptr = c->buffer;

        c->buffer_end = c->buffer;

        return;

    }

    url_fprintf(pb, "HTTP/1.0 200 OK\r\n");

    url_fprintf(pb, "Content-type: %s\r\n", "text/html");

    url_fprintf(pb, "Pragma: no-cache\r\n");

    url_fprintf(pb, "\r\n");

    url_fprintf(pb, "<TITLE>FFServer Status</TITLE>\n");

    if (c->stream->feed_filename)

        url_fprintf(pb, "<link rel=\"shortcut icon\" href=\"%s\">\n", c->stream->feed_filename);

    url_fprintf(pb, "\n");

    url_fprintf(pb, "
FFServer Status
\n");

    /* format status */

    url_fprintf(pb, "
Available Streams
\n");

    url_fprintf(pb, "
\n");

    url_fprintf(pb, "
PathServed
Conns
bytesFormatBit rate
kbits/sVideo
kbits/s
CodecAudio
kbits/s
CodecFeed\n");

    stream = first_stream;

    while (stream != NULL) {

        char sfilename[1024];

        char *eosf;

        if (stream->feed != stream) {

            av_strlcpy(sfilename, stream->filename, sizeof(sfilename) - 10);

            eosf = sfilename + strlen(sfilename);

            if (eosf - sfilename >= 4) {

                if (strcmp(eosf - 4, ".asf") == 0)

                    strcpy(eosf - 4, ".asx");

                else if (strcmp(eosf - 3, ".rm") == 0)

                    strcpy(eosf - 3, ".ram");

                else if (!strcmp(stream->fmt->name, "rtp")) {

                    /* generate a sample RTSP director if

                       unicast. Generate an SDP redirector if

                       multicast */

                    eosf = strrchr(sfilename, '.');

                    if (!eosf)

                        eosf = sfilename + strlen(sfilename);

                    if (stream->is_multicast)

                        strcpy(eosf, ".sdp");

                    else

                        strcpy(eosf, ".rtsp");

                }

            }

            url_fprintf(pb, "
%s ",

                         sfilename, stream->filename);

            url_fprintf(pb, " %d  ",

                        stream->conns_served);

            fmt_bytecount(pb, stream->bytes_served);

            switch(stream->stream_type) {

            case STREAM_TYPE_LIVE:

                {

                    int audio_bit_rate = 0;

                    int video_bit_rate = 0;

                    const char *audio_codec_name = "";

                    const char *video_codec_name = "";

                    const char *audio_codec_name_extra = "";

                    const char *video_codec_name_extra = "";

                    for(i=0;i<stream->nb_streams;i++) {

                        AVStream *st = stream->streams[i];

                        AVCodec *codec = avcodec_find_encoder(st->codec->codec_id);

                        switch(st->codec->codec_type) {

                        case CODEC_TYPE_AUDIO:

                            audio_bit_rate += st->codec->bit_rate;

                            if (codec) {

                                if (*audio_codec_name)

                                    audio_codec_name_extra = "...";

                                audio_codec_name = codec->name;

                            }

                            break;

                        case CODEC_TYPE_VIDEO:

                            video_bit_rate += st->codec->bit_rate;

                            if (codec) {

                                if (*video_codec_name)

                                    video_codec_name_extra = "...";

                                video_codec_name = codec->name;

                            }

                            break;

                        case CODEC_TYPE_DATA:

                            video_bit_rate += st->codec->bit_rate;

                            break;

                        default:

                            abort();

                        }

                    }

                    url_fprintf(pb, " %s  %d  %d  %s %s  %d  %s %s",

                                 stream->fmt->name,

                                 stream->bandwidth,

                                 video_bit_rate / 1000, video_codec_name, video_codec_name_extra,

                                 audio_bit_rate / 1000, audio_codec_name, audio_codec_name_extra);

                    if (stream->feed)

                        url_fprintf(pb, "%s", stream->feed->filename);

                    else

                        url_fprintf(pb, "%s", stream->feed_filename);

                    url_fprintf(pb, "\n");

                }

                break;

            default:

                url_fprintf(pb, " -  -  -  - \n");

                break;

            }

        }

        stream = stream->next;

    }

    url_fprintf(pb, "
\n");

    stream = first_stream;

    while (stream != NULL) {

        if (stream->feed == stream) {

            url_fprintf(pb, "
Feed %s
", stream->filename);

            if (stream->pid) {

                url_fprintf(pb, "Running as pid %d.\n", stream->pid);

#if defined(linux) && !defined(CONFIG_NOCUTILS)

                {

                    FILE *pid_stat;

                    char ps_cmd[64];

                    /* This is somewhat linux specific I guess */

                    snprintf(ps_cmd, sizeof(ps_cmd),

                             "ps -o \"%%cpu,cputime\" --no-headers %d",

                             stream->pid);

                    pid_stat = popen(ps_cmd, "r");

                    if (pid_stat) {

                        char cpuperc[10];

                        char cpuused[64];

                        if (fscanf(pid_stat, "%10s %64s", cpuperc,

                                   cpuused) == 2) {

                            url_fprintf(pb, "Currently using %s%% of the cpu. Total time used %s.\n",

                                         cpuperc, cpuused);

                        }

                        fclose(pid_stat);

                    }

                }

#endif

                url_fprintf(pb, "
");

            }

            url_fprintf(pb, "

Streamtypekbits/scodecParameters\n");

            for (i = 0; i < stream->nb_streams; i++) {

                AVStream *st = stream->streams[i];

                AVCodec *codec = avcodec_find_encoder(st->codec->codec_id);

                const char *type = "unknown";

                char parameters[64];

                parameters[0] = 0;

                switch(st->codec->codec_type) {

                case CODEC_TYPE_AUDIO:

                    type = "audio";

                    snprintf(parameters, sizeof(parameters), "%d channel(s), %d Hz", st->codec->channels, st->codec->sample_rate);

                    break;

                case CODEC_TYPE_VIDEO:

                    type = "video";

                    snprintf(parameters, sizeof(parameters), "%dx%d, q=%d-%d, fps=%d", st->codec->width, st->codec->height,

                                st->codec->qmin, st->codec->qmax, st->codec->time_base.den / st->codec->time_base.num);

                    break;

                default:

                    abort();

                }

                url_fprintf(pb, "
%d%s%d%s%s\n",

                        i, type, st->codec->bit_rate/1000, codec ? codec->name : "", parameters);

            }

            url_fprintf(pb, "
\n");

        }

        stream = stream->next;

    }

#if 0

    {

        float avg;

        AVCodecContext *enc;

        char buf[1024];

        /* feed status */

        stream = first_feed;

        while (stream != NULL) {

            url_fprintf(pb, "
Feed '%s'
\n", stream->filename);

            url_fprintf(pb, "
\n");

            url_fprintf(pb, "
ParametersFrame countSizeAvg bitrate (kbits/s)\n");

            for(i=0;i<stream->nb_streams;i++) {

                AVStream *st = stream->streams[i];

                FeedData *fdata = st->priv_data;

                enc = st->codec;

                avcodec_string(buf, sizeof(buf), enc);

                avg = fdata->avg_frame_size * (float)enc->rate * 8.0;

                if (enc->codec->type == CODEC_TYPE_AUDIO && enc->frame_size > 0)

                    avg /= enc->frame_size;

                url_fprintf(pb, "
%s  %d  %"PRId64"  %0.1f\n",

                             buf, enc->frame_number, fdata->data_count, avg / 1000.0);

            }

            url_fprintf(pb, "
\n");

            stream = stream->next_feed;

        }

    }

#endif

    /* connection status */

    url_fprintf(pb, "
Connection Status
\n");

    url_fprintf(pb, "Number of connections: %d / %d
\n",

                 nb_connections, nb_max_connections);

    url_fprintf(pb, "Bandwidth in use: %dk / %dk
\n",

                 current_bandwidth, max_bandwidth);

    url_fprintf(pb, "
\n");

    url_fprintf(pb, "
#FileIPProtoStateTarget bits/secActual bits/secBytes transferred\n");

    c1 = first_http_ctx;

    i = 0;

    while (c1 != NULL) {

        int bitrate;

        int j;

        bitrate = 0;

        if (c1->stream) {

            for (j = 0; j < c1->stream->nb_streams; j++) {

                if (!c1->stream->feed)

                    bitrate += c1->stream->streams[j]->codec->bit_rate;

                else if (c1->feed_streams[j] >= 0)

                    bitrate += c1->stream->feed->streams[c1->feed_streams[j]]->codec->bit_rate;

            }

        }

        i++;

        p = inet_ntoa(c1->from_addr.sin_addr);

        url_fprintf(pb, "
%d%s%s%s%s%s",

                    i,

                    c1->stream ? c1->stream->filename : "",

                    c1->state == HTTPSTATE_RECEIVE_DATA ? "(input)" : "",

                    p,

                    c1->protocol,

                    http_state[c1->state]);

        fmt_bytecount(pb, bitrate);

        url_fprintf(pb, "");

        fmt_bytecount(pb, compute_datarate(&c1->datarate, c1->data_count) * 8);

        url_fprintf(pb, "");

        fmt_bytecount(pb, c1->data_count);

        url_fprintf(pb, "\n");

        c1 = c1->next;

    }

    url_fprintf(pb, "
\n");

    /* date */

    ti = time(NULL);

    p = ctime(&ti);

    url_fprintf(pb, "
Generated at %s", p);

    url_fprintf(pb, "\n\n");

    len = url_close_dyn_buf(pb, &c->pb_buffer);

    c->buffer_ptr = c->pb_buffer;

    c->buffer_end = c->pb_buffer + len;

}


void gen_intermediate_code(CPUState *cs, TranslationBlock *tb)

{

    CPUM68KState *env = cs->env_ptr;

    DisasContext dc1, *dc = &dc1;

    target_ulong pc_start;

    int pc_offset;

    int num_insns;

    int max_insns;

    /* generate intermediate code */

    pc_start = tb->pc;

    dc->tb = tb;

    dc->env = env;

    dc->is_jmp = DISAS_NEXT;

    dc->pc = pc_start;

    dc->cc_op = CC_OP_DYNAMIC;

    dc->cc_op_synced = 1;

    dc->singlestep_enabled = cs->singlestep_enabled;

    dc->user = (env->sr & SR_S) == 0;

    dc->done_mac = 0;

    dc->writeback_mask = 0;

    num_insns = 0;

    max_insns = tb->cflags & CF_COUNT_MASK;

    if (max_insns == 0) {

        max_insns = CF_COUNT_MASK;

    }

    if (max_insns > TCG_MAX_INSNS) {

        max_insns = TCG_MAX_INSNS;

    }

    gen_tb_start(tb);

    do {

        pc_offset = dc->pc - pc_start;

        gen_throws_exception = NULL;

        tcg_gen_insn_start(dc->pc, dc->cc_op);

        num_insns++;

        if (unlikely(cpu_breakpoint_test(cs, dc->pc, BP_ANY))) {

            gen_exception(dc, dc->pc, EXCP_DEBUG);

            dc->is_jmp = DISAS_JUMP;

            /* The address covered by the breakpoint must be included in

               [tb->pc, tb->pc + tb->size) in order to for it to be

               properly cleared -- thus we increment the PC here so that

               the logic setting tb->size below does the right thing.  */

            dc->pc += 2;

            break;

        }

        if (num_insns == max_insns && (tb->cflags & CF_LAST_IO)) {

            gen_io_start();

        }

        dc->insn_pc = dc->pc;

	disas_m68k_insn(env, dc);

    } while (!dc->is_jmp && !tcg_op_buf_full() &&

             !cs->singlestep_enabled &&

             !singlestep &&

             (pc_offset) < (TARGET_PAGE_SIZE - 32) &&

             num_insns < max_insns);

    if (tb->cflags & CF_LAST_IO)

        gen_io_end();

    if (unlikely(cs->singlestep_enabled)) {

        /* Make sure the pc is updated, and raise a debug exception.  */

        if (!dc->is_jmp) {

            update_cc_op(dc);

            tcg_gen_movi_i32(QREG_PC, dc->pc);

        }

        gen_helper_raise_exception(cpu_env, tcg_const_i32(EXCP_DEBUG));

    } else {

        switch(dc->is_jmp) {

        case DISAS_NEXT:

            update_cc_op(dc);

            gen_jmp_tb(dc, 0, dc->pc);

            break;

        default:

        case DISAS_JUMP:

        case DISAS_UPDATE:

            update_cc_op(dc);

            /* indicate that the hash table must be used to find the next TB */

            tcg_gen_exit_tb(0);

            break;

        case DISAS_TB_JUMP:

            /* nothing more to generate */

            break;

        }

    }

    gen_tb_end(tb, num_insns);

#ifdef DEBUG_DISAS

    if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)

        && qemu_log_in_addr_range(pc_start)) {

        qemu_log_lock();

        qemu_log("----------------\n");

        qemu_log("IN: %s\n", lookup_symbol(pc_start));

        log_target_disas(cs, pc_start, dc->pc - pc_start, 0);

        qemu_log("\n");

        qemu_log_unlock();

    }

#endif

    tb->size = dc->pc - pc_start;

    tb->icount = num_insns;

}


BdrvChild *bdrv_open_child(const char *filename,

                           QDict *options, const char *bdref_key,

                           BlockDriverState* parent,

                           const BdrvChildRole *child_role,

                           bool allow_none, Error **errp)

{

    BdrvChild *c = NULL;

    BlockDriverState *bs;

    QDict *image_options;

    int ret;

    char *bdref_key_dot;

    const char *reference;

    assert(child_role != NULL);

    bdref_key_dot = g_strdup_printf("%s.", bdref_key);

    qdict_extract_subqdict(options, &image_options, bdref_key_dot);

    g_free(bdref_key_dot);

    reference = qdict_get_try_str(options, bdref_key);

    if (!filename && !reference && !qdict_size(image_options)) {

        if (!allow_none) {

            error_setg(errp, "A block device must be specified for \"%s\"",

                       bdref_key);

        }

        QDECREF(image_options);

        goto done;

    }

    bs = NULL;

    ret = bdrv_open_inherit(&bs, filename, reference, image_options, 0,

                            parent, child_role, errp);

    if (ret < 0) {

        goto done;

    }

    c = bdrv_attach_child(parent, bs, child_role);

done:

    qdict_del(options, bdref_key);

    return c;

}


av_cold void ff_videodsp_init_x86(VideoDSPContext *ctx, int bpc)

{

#if HAVE_YASM

    int cpu_flags = av_get_cpu_flags();

#if ARCH_X86_32

    if (EXTERNAL_MMX(cpu_flags) && bpc <= 8) {

        ctx->emulated_edge_mc = emulated_edge_mc_mmx;

    }

    if (EXTERNAL_AMD3DNOW(cpu_flags)) {

        ctx->prefetch = ff_prefetch_3dnow;

    }

#endif /* ARCH_X86_32 */

    if (EXTERNAL_MMXEXT(cpu_flags)) {

        ctx->prefetch = ff_prefetch_mmxext;

#if ARCH_X86_32

        if (bpc <= 8)

            ctx->emulated_edge_mc = emulated_edge_mc_mmxext;

#endif /* ARCH_X86_32 */

    }

#if ARCH_X86_32

    if (EXTERNAL_SSE(cpu_flags) && bpc <= 8) {

        ctx->emulated_edge_mc = emulated_edge_mc_sse;

    }

#endif /* ARCH_X86_32 */

    if (EXTERNAL_SSE2(cpu_flags) && bpc <= 8) {

        ctx->emulated_edge_mc = emulated_edge_mc_sse2;

    }

#endif /* HAVE_YASM */

}


static void vapic_write(void *opaque, hwaddr addr, uint64_t data,

                        unsigned int size)

{

    CPUState *cs = current_cpu;

    X86CPU *cpu = X86_CPU(cs);

    CPUX86State *env = &cpu->env;

    hwaddr rom_paddr;

    VAPICROMState *s = opaque;

    cpu_synchronize_state(cs);

    /*

     * The VAPIC supports two PIO-based hypercalls, both via port 0x7E.

     *  o 16-bit write access:

     *    Reports the option ROM initialization to the hypervisor. Written

     *    value is the offset of the state structure in the ROM.

     *  o 8-bit write access:

     *    Reactivates the VAPIC after a guest hibernation, i.e. after the

     *    option ROM content has been re-initialized by a guest power cycle.

     *  o 32-bit write access:

     *    Poll for pending IRQs, considering the current VAPIC state.

     */

    switch (size) {

    case 2:

        if (s->state == VAPIC_INACTIVE) {

            rom_paddr = (env->segs[R_CS].base + env->eip) & ROM_BLOCK_MASK;

            s->rom_state_paddr = rom_paddr + data;

            s->state = VAPIC_STANDBY;

        }

        if (vapic_prepare(s) < 0) {

            s->state = VAPIC_INACTIVE;

            s->rom_state_paddr = 0;

            break;

        }

        break;

    case 1:

        if (kvm_enabled()) {

            /*

             * Disable triggering instruction in ROM by writing a NOP.

             *

             * We cannot do this in TCG mode as the reported IP is not

             * accurate.

             */

            pause_all_vcpus();

            patch_byte(cpu, env->eip - 2, 0x66);

            patch_byte(cpu, env->eip - 1, 0x90);

            resume_all_vcpus();

        }

        if (s->state == VAPIC_ACTIVE) {

            break;

        }

        if (update_rom_mapping(s, env, env->eip) < 0) {

            break;

        }

        if (find_real_tpr_addr(s, env) < 0) {

            break;

        }

        vapic_enable(s, cpu);

        break;

    default:

    case 4:

        if (!kvm_irqchip_in_kernel()) {

            apic_poll_irq(cpu->apic_state);

        }

        break;

    }

}


static bool riccb_needed(void *opaque)

{

#ifdef CONFIG_KVM

    if (kvm_enabled()) {

        return kvm_s390_get_ri();

    }

#endif

    return 0;

}


static av_always_inline void decode_cabac_luma_residual(const H264Context *h, H264SliceContext *sl,

                                                        const uint8_t *scan, const uint8_t *scan8x8,

                                                        int pixel_shift, int mb_type, int cbp, int p)

{

    static const uint8_t ctx_cat[4][3] = {{0,6,10},{1,7,11},{2,8,12},{5,9,13}};

    const uint32_t *qmul;

    int i8x8, i4x4;

    int qscale = p == 0 ? sl->qscale : sl->chroma_qp[p - 1];

    if( IS_INTRA16x16( mb_type ) ) {

        AV_ZERO128(sl->mb_luma_dc[p]+0);

        AV_ZERO128(sl->mb_luma_dc[p]+8);

        AV_ZERO128(sl->mb_luma_dc[p]+16);

        AV_ZERO128(sl->mb_luma_dc[p]+24);

        decode_cabac_residual_dc(h, sl, sl->mb_luma_dc[p], ctx_cat[0][p], LUMA_DC_BLOCK_INDEX+p, scan, 16);

        if( cbp&15 ) {

            qmul = h->dequant4_coeff[p][qscale];

            for( i4x4 = 0; i4x4 < 16; i4x4++ ) {

                const int index = 16*p + i4x4;

                decode_cabac_residual_nondc(h, sl, sl->mb + (16*index << pixel_shift), ctx_cat[1][p], index, scan + 1, qmul, 15);

            }

        } else {

            fill_rectangle(&sl->non_zero_count_cache[scan8[16*p]], 4, 4, 8, 0, 1);

        }

    } else {

        int cqm = (IS_INTRA( mb_type ) ? 0:3) + p;

        for( i8x8 = 0; i8x8 < 4; i8x8++ ) {

            if( cbp & (1<<i8x8) ) {

                if( IS_8x8DCT(mb_type) ) {

                    const int index = 16*p + 4*i8x8;

                    decode_cabac_residual_nondc(h, sl, sl->mb + (16*index << pixel_shift), ctx_cat[3][p], index,

                                                scan8x8, h->dequant8_coeff[cqm][qscale], 64);

                } else {

                    qmul = h->dequant4_coeff[cqm][qscale];

                    for( i4x4 = 0; i4x4 < 4; i4x4++ ) {

                        const int index = 16*p + 4*i8x8 + i4x4;

//START_TIMER

                        decode_cabac_residual_nondc(h, sl, sl->mb + (16*index << pixel_shift), ctx_cat[2][p], index, scan, qmul, 16);

//STOP_TIMER("decode_residual")

                    }

                }

            } else {

                fill_rectangle(&sl->non_zero_count_cache[scan8[4*i8x8+16*p]], 2, 2, 8, 0, 1);

            }

        }

    }

}


static int virtio_ccw_load_config(DeviceState *d, QEMUFile *f)

{

    VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d);

    CcwDevice *ccw_dev = CCW_DEVICE(d);

    CCWDeviceClass *ck = CCW_DEVICE_GET_CLASS(ccw_dev);

    SubchDev *s = ccw_dev->sch;

    VirtIODevice *vdev = virtio_ccw_get_vdev(s);

    int len;

    s->driver_data = dev;

    subch_device_load(s, f);

    /* Re-fill subch_id after loading the subchannel states.*/

    if (ck->refill_ids) {

        ck->refill_ids(ccw_dev);

    }

    len = qemu_get_be32(f);

    if (len != 0) {

        dev->indicators = get_indicator(qemu_get_be64(f), len);

    } else {

        qemu_get_be64(f);

        dev->indicators = NULL;

    }

    len = qemu_get_be32(f);

    if (len != 0) {

        dev->indicators2 = get_indicator(qemu_get_be64(f), len);

    } else {

        qemu_get_be64(f);

        dev->indicators2 = NULL;

    }

    len = qemu_get_be32(f);

    if (len != 0) {

        dev->summary_indicator = get_indicator(qemu_get_be64(f), len);

    } else {

        qemu_get_be64(f);

        dev->summary_indicator = NULL;

    }

    qemu_get_be16s(f, &vdev->config_vector);

    dev->routes.adapter.ind_offset = qemu_get_be64(f);

    dev->thinint_isc = qemu_get_byte(f);

    dev->revision = qemu_get_be32(f);

    if (s->thinint_active) {

        return css_register_io_adapter(CSS_IO_ADAPTER_VIRTIO,

                                       dev->thinint_isc, true, false,

                                       &dev->routes.adapter.adapter_id);

    }

    return 0;

}


PCIBus *pci_pmac_u3_init(qemu_irq *pic,

                         MemoryRegion *address_space_mem,

                         MemoryRegion *address_space_io)

{

    DeviceState *dev;

    SysBusDevice *s;

    PCIHostState *h;

    UNINState *d;

    /* Uninorth AGP bus */

    dev = qdev_create(NULL, TYPE_U3_AGP_HOST_BRIDGE);

    qdev_init_nofail(dev);

    s = SYS_BUS_DEVICE(dev);

    h = PCI_HOST_BRIDGE(dev);

    d = U3_AGP_HOST_BRIDGE(dev);

    memory_region_init(&d->pci_mmio, OBJECT(d), "pci-mmio", 0x100000000ULL);

    memory_region_init_alias(&d->pci_hole, OBJECT(d), "pci-hole", &d->pci_mmio,

                             0x80000000ULL, 0x70000000ULL);

    memory_region_add_subregion(address_space_mem, 0x80000000ULL,

                                &d->pci_hole);

    h->bus = pci_register_bus(dev, "pci",

                              pci_unin_set_irq, pci_unin_map_irq,

                              pic,

                              &d->pci_mmio,

                              address_space_io,

                              PCI_DEVFN(11, 0), 4, TYPE_PCI_BUS);

    sysbus_mmio_map(s, 0, 0xf0800000);

    sysbus_mmio_map(s, 1, 0xf0c00000);

    pci_create_simple(h->bus, 11 << 3, "u3-agp");

    return h->bus;

}


static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *samplesref)

{

    AVFilterContext *ctx = inlink->dst;

    int i;

    for (i = 0; i < ctx->nb_outputs; i++)

        ff_filter_samples(inlink->dst->outputs[i],

                          avfilter_ref_buffer(samplesref, ~AV_PERM_WRITE));

}


void coroutine_fn qemu_co_mutex_lock(CoMutex *mutex)

{

    Coroutine *self = qemu_coroutine_self();

    trace_qemu_co_mutex_lock_entry(mutex, self);

    while (mutex->locked) {

        qemu_co_queue_wait(&mutex->queue);

    }

    mutex->locked = true;

    trace_qemu_co_mutex_lock_return(mutex, self);

}


void hmp_info_block(Monitor *mon, const QDict *qdict)

{

    BlockInfoList *block_list, *info;

    ImageInfo *image_info;

    const char *device = qdict_get_try_str(qdict, "device");

    bool verbose = qdict_get_try_bool(qdict, "verbose", 0);

    block_list = qmp_query_block(NULL);

    for (info = block_list; info; info = info->next) {

        if (device && strcmp(device, info->value->device)) {

            continue;

        }

        if (info != block_list) {

            monitor_printf(mon, "\n");

        }

        monitor_printf(mon, "%s", info->value->device);

        if (info->value->has_inserted) {

            monitor_printf(mon, ": %s (%s%s%s)\n",

                           info->value->inserted->file,

                           info->value->inserted->drv,

                           info->value->inserted->ro ? ", read-only" : "",

                           info->value->inserted->encrypted ? ", encrypted" : "");

        } else {

            monitor_printf(mon, ": [not inserted]\n");

        }

        if (info->value->has_io_status && info->value->io_status != BLOCK_DEVICE_IO_STATUS_OK) {

            monitor_printf(mon, "    I/O status:       %s\n",

                           BlockDeviceIoStatus_lookup[info->value->io_status]);

        }

        if (info->value->removable) {

            monitor_printf(mon, "    Removable device: %slocked, tray %s\n",

                           info->value->locked ? "" : "not ",

                           info->value->tray_open ? "open" : "closed");

        }

        if (!info->value->has_inserted) {

            continue;

        }

        if (info->value->inserted->has_backing_file) {

            monitor_printf(mon,

                           "    Backing file:     %s "

                           "(chain depth: %" PRId64 ")\n",

                           info->value->inserted->backing_file,

                           info->value->inserted->backing_file_depth);

        }

        if (info->value->inserted->bps

            || info->value->inserted->bps_rd

            || info->value->inserted->bps_wr

            || info->value->inserted->iops

            || info->value->inserted->iops_rd

            || info->value->inserted->iops_wr)

        {

            monitor_printf(mon, "    I/O throttling:   bps=%" PRId64

                            " bps_rd=%" PRId64  " bps_wr=%" PRId64

                            " iops=%" PRId64 " iops_rd=%" PRId64

                            " iops_wr=%" PRId64 "\n",

                            info->value->inserted->bps,

                            info->value->inserted->bps_rd,

                            info->value->inserted->bps_wr,

                            info->value->inserted->iops,

                            info->value->inserted->iops_rd,

                            info->value->inserted->iops_wr);

        }

        if (verbose) {

            monitor_printf(mon, "\nImages:\n");

            image_info = info->value->inserted->image;

            while (1) {

                    bdrv_image_info_dump((fprintf_function)monitor_printf,

                                         mon, image_info);

                if (image_info->has_backing_image) {

                    image_info = image_info->backing_image;

                } else {

                    break;

                }

            }

        }

    }

    qapi_free_BlockInfoList(block_list);

}


static inline int decode_residual_inter(AVSContext *h) {

    int block;

    /* get coded block pattern */

    int cbp= get_ue_golomb(&h->s.gb);

    if(cbp > 63){

        av_log(h->s.avctx, AV_LOG_ERROR, "illegal inter cbp\n");

        return -1;

    }

    h->cbp = cbp_tab[cbp][1];

    /* get quantizer */

    if(h->cbp && !h->qp_fixed)

        h->qp = (h->qp + get_se_golomb(&h->s.gb)) & 63;

    for(block=0;block<4;block++)

        if(h->cbp & (1<<block))

            decode_residual_block(h,&h->s.gb,ff_cavs_inter_dec,0,h->qp,

                                  h->cy + h->luma_scan[block], h->l_stride);

    decode_residual_chroma(h);

    return 0;

}


static void fw_cfg_write(FWCfgState *s, uint8_t value)

{

    int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL);

    FWCfgEntry *e = &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK];

    FW_CFG_DPRINTF("write %d\n", value);

    if (s->cur_entry & FW_CFG_WRITE_CHANNEL && s->cur_offset < e->len) {

        e->data[s->cur_offset++] = value;

        if (s->cur_offset == e->len) {

            e->callback(e->callback_opaque, e->data);

            s->cur_offset = 0;

        }

    }

}


int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset)

{

    int pending;

    int index;

    assert(!qemu_file_is_writable(f));

    index = f->buf_index + offset;

    pending = f->buf_size - index;

    if (pending < size) {

        qemu_fill_buffer(f);

        index = f->buf_index + offset;

        pending = f->buf_size - index;

    }

    if (pending <= 0) {

        return 0;

    }

    if (size > pending) {

        size = pending;

    }

    memcpy(buf, f->buf + index, size);

    return size;

}


static int ahci_dma_rw_buf(IDEDMA *dma, int is_write)

{

    AHCIDevice *ad = DO_UPCAST(AHCIDevice, dma, dma);

    IDEState *s = &ad->port.ifs[0];

    uint8_t *p = s->io_buffer + s->io_buffer_index;

    int l = s->io_buffer_size - s->io_buffer_index;

    if (ahci_populate_sglist(ad, &s->sg)) {

        return 0;

    }

    if (is_write) {

        dma_buf_read(p, l, &s->sg);

    } else {

        dma_buf_write(p, l, &s->sg);

    }

    /* update number of transferred bytes */

    ad->cur_cmd->status = cpu_to_le32(le32_to_cpu(ad->cur_cmd->status) + l);

    s->io_buffer_index += l;

    DPRINTF(ad->port_no, "len=%#x\n", l);

    return 1;

}


void cpu_save(QEMUFile *f, void *opaque)

{

    CPUState *env = (CPUState *)opaque;

    unsigned int i, j;

    cpu_synchronize_state(env);

    for (i = 0; i < 32; i++)

        qemu_put_betls(f, &env->gpr[i]);

#if !defined(TARGET_PPC64)

    for (i = 0; i < 32; i++)

        qemu_put_betls(f, &env->gprh[i]);

#endif

    qemu_put_betls(f, &env->lr);

    qemu_put_betls(f, &env->ctr);

    for (i = 0; i < 8; i++)

        qemu_put_be32s(f, &env->crf[i]);

    qemu_put_betls(f, &env->xer);

    qemu_put_betls(f, &env->reserve_addr);

    qemu_put_betls(f, &env->msr);

    for (i = 0; i < 4; i++)

        qemu_put_betls(f, &env->tgpr[i]);

    for (i = 0; i < 32; i++) {

        union {

            float64 d;

            uint64_t l;

        } u;

        u.d = env->fpr[i];

        qemu_put_be64(f, u.l);

    }

    qemu_put_be32s(f, &env->fpscr);

    qemu_put_sbe32s(f, &env->access_type);

#if !defined(CONFIG_USER_ONLY)

#if defined(TARGET_PPC64)

    qemu_put_betls(f, &env->asr);

    qemu_put_sbe32s(f, &env->slb_nr);

#endif

    qemu_put_betls(f, &env->sdr1);

    for (i = 0; i < 32; i++)

        qemu_put_betls(f, &env->sr[i]);

    for (i = 0; i < 2; i++)

        for (j = 0; j < 8; j++)

            qemu_put_betls(f, &env->DBAT[i][j]);

    for (i = 0; i < 2; i++)

        for (j = 0; j < 8; j++)

            qemu_put_betls(f, &env->IBAT[i][j]);

    qemu_put_sbe32s(f, &env->nb_tlb);

    qemu_put_sbe32s(f, &env->tlb_per_way);

    qemu_put_sbe32s(f, &env->nb_ways);

    qemu_put_sbe32s(f, &env->last_way);

    qemu_put_sbe32s(f, &env->id_tlbs);

    qemu_put_sbe32s(f, &env->nb_pids);

    if (env->tlb) {

        // XXX assumes 6xx

        for (i = 0; i < env->nb_tlb; i++) {

            qemu_put_betls(f, &env->tlb[i].tlb6.pte0);

            qemu_put_betls(f, &env->tlb[i].tlb6.pte1);

            qemu_put_betls(f, &env->tlb[i].tlb6.EPN);

        }

    }

    for (i = 0; i < 4; i++)

        qemu_put_betls(f, &env->pb[i]);

#endif

    for (i = 0; i < 1024; i++)

        qemu_put_betls(f, &env->spr[i]);

    qemu_put_be32s(f, &env->vscr);

    qemu_put_be64s(f, &env->spe_acc);

    qemu_put_be32s(f, &env->spe_fscr);

    qemu_put_betls(f, &env->msr_mask);

    qemu_put_be32s(f, &env->flags);

    qemu_put_sbe32s(f, &env->error_code);

    qemu_put_be32s(f, &env->pending_interrupts);

#if !defined(CONFIG_USER_ONLY)

    qemu_put_be32s(f, &env->irq_input_state);

    for (i = 0; i < POWERPC_EXCP_NB; i++)

        qemu_put_betls(f, &env->excp_vectors[i]);

    qemu_put_betls(f, &env->excp_prefix);

    qemu_put_betls(f, &env->hreset_excp_prefix);

    qemu_put_betls(f, &env->ivor_mask);

    qemu_put_betls(f, &env->ivpr_mask);

    qemu_put_betls(f, &env->hreset_vector);

#endif

    qemu_put_betls(f, &env->nip);

    qemu_put_betls(f, &env->hflags);

    qemu_put_betls(f, &env->hflags_nmsr);

    qemu_put_sbe32s(f, &env->mmu_idx);

    qemu_put_sbe32s(f, &env->power_mode);

}


static DeviceState *sun4c_intctl_init(target_phys_addr_t addr,

                                      qemu_irq *parent_irq)

{

    DeviceState *dev;

    SysBusDevice *s;

    unsigned int i;

    dev = qdev_create(NULL, "sun4c_intctl");

    qdev_init(dev);

    s = sysbus_from_qdev(dev);

    for (i = 0; i < MAX_PILS; i++) {

        sysbus_connect_irq(s, i, parent_irq[i]);

    }

    sysbus_mmio_map(s, 0, addr);

    return dev;

}


static int gif_image_write_header(AVFormatContext *s, int width, int height,

                                  int loop_count, uint32_t *palette)

{

    AVIOContext *pb = s->pb;

    AVRational sar = s->streams[0]->codec->sample_aspect_ratio;

    int i, aspect = 0;

    if (sar.num > 0 && sar.den > 0) {

        aspect = sar.num * 64 / sar.den - 15;

        if (aspect < 0 || aspect > 255)

            aspect = 0;

    }

    avio_write(pb, "GIF", 3);

    avio_write(pb, "89a", 3);

    avio_wl16(pb, width);

    avio_wl16(pb, height);

    if (palette) {

        avio_w8(pb, 0xf7); /* flags: global clut, 256 entries */

        avio_w8(pb, 0x1f); /* background color index */

        avio_w8(pb, aspect);

        for (i = 0; i < 256; i++) {

            const uint32_t v = palette[i] & 0xffffff;

            avio_wb24(pb, v);

        }

    } else {

        avio_w8(pb, 0); /* flags */

        avio_w8(pb, 0); /* background color index */

        avio_w8(pb, aspect);

    }

    if (loop_count >= 0 ) {

        /* "NETSCAPE EXTENSION" for looped animation GIF */

        avio_w8(pb, 0x21); /* GIF Extension code */

        avio_w8(pb, 0xff); /* Application Extension Label */

        avio_w8(pb, 0x0b); /* Length of Application Block */

        avio_write(pb, "NETSCAPE2.0", sizeof("NETSCAPE2.0") - 1);

        avio_w8(pb, 0x03); /* Length of Data Sub-Block */

        avio_w8(pb, 0x01);

        avio_wl16(pb, (uint16_t)loop_count);

        avio_w8(pb, 0x00); /* Data Sub-block Terminator */

    }

    return 0;

}


static int ipvideo_decode_block_opcode_0xA(IpvideoContext *s)

{

    int x, y;

    unsigned char P[4];

    int flags = 0;

    /* 4-color encoding for each 4x4 quadrant, or 4-color encoding on

     * either top and bottom or left and right halves */

    CHECK_STREAM_PTR(24);

    if (s->stream_ptr[0] <= s->stream_ptr[1]) {

        /* 4-color encoding for each quadrant; need 32 bytes */

        CHECK_STREAM_PTR(32);

        for (y = 0; y < 16; y++) {

            // new values for each 4x4 block

            if (!(y & 3)) {

                memcpy(P, s->stream_ptr, 4);

                s->stream_ptr += 4;

                flags = bytestream_get_le32(&s->stream_ptr);

            }

            for (x = 0; x < 4; x++, flags >>= 2)

                *s->pixel_ptr++ = P[flags & 0x03];

            s->pixel_ptr += s->stride - 4;

            // switch to right half

            if (y == 7) s->pixel_ptr -= 8 * s->stride - 4;

        }

    } else {

        // vertical split?

        int vert = s->stream_ptr[12] <= s->stream_ptr[13];

        uint64_t flags = 0;

        /* 4-color encoding for either left and right or top and bottom

         * halves */

        for (y = 0; y < 16; y++) {

            // load values for each half

            if (!(y & 7)) {

                memcpy(P, s->stream_ptr, 4);

                s->stream_ptr += 4;

                flags = bytestream_get_le64(&s->stream_ptr);

            }

            for (x = 0; x < 4; x++, flags >>= 2)

                *s->pixel_ptr++ = P[flags & 0x03];

            if (vert) {

                s->pixel_ptr += s->stride - 4;

                // switch to right half

                if (y == 7) s->pixel_ptr -= 8 * s->stride - 4;

            } else if (y & 1) s->pixel_ptr += s->line_inc;

        }

    }

    /* report success */

    return 0;

}


static inline void dv_decode_video_segment(DVVideoContext *s,

                                           const uint8_t *buf_ptr1,

                                           const uint16_t *mb_pos_ptr)

{

    int quant, dc, dct_mode, class1, j;

    int mb_index, mb_x, mb_y, v, last_index;

    int y_stride, i;

    DCTELEM *block, *block1;

    int c_offset;

    uint8_t *y_ptr;

    const uint8_t *buf_ptr;

    PutBitContext pb, vs_pb;

    GetBitContext gb;

    BlockInfo mb_data[5 * DV_MAX_BPM], *mb, *mb1;

    DECLARE_ALIGNED_16(DCTELEM, sblock[5*DV_MAX_BPM][64]);

    DECLARE_ALIGNED_8(uint8_t, mb_bit_buffer[80 + 4]); /* allow some slack */

    DECLARE_ALIGNED_8(uint8_t, vs_bit_buffer[5 * 80 + 4]); /* allow some slack */

    const int log2_blocksize= 3-s->avctx->lowres;

    int is_field_mode[5];

    assert((((int)mb_bit_buffer)&7)==0);

    assert((((int)vs_bit_buffer)&7)==0);

    memset(sblock, 0, sizeof(sblock));

    /* pass 1 : read DC and AC coefficients in blocks */

    buf_ptr = buf_ptr1;

    block1 = &sblock[0][0];

    mb1 = mb_data;

    init_put_bits(&vs_pb, vs_bit_buffer, 5 * 80);

    for(mb_index = 0; mb_index < 5; mb_index++, mb1 += s->sys->bpm, block1 += s->sys->bpm * 64) {

        /* skip header */

        quant = buf_ptr[3] & 0x0f;

        buf_ptr += 4;

        init_put_bits(&pb, mb_bit_buffer, 80);

        mb = mb1;

        block = block1;

        is_field_mode[mb_index] = 0;

        for(j = 0;j < s->sys->bpm; j++) {

            last_index = s->sys->block_sizes[j];

            init_get_bits(&gb, buf_ptr, last_index);

            /* get the dc */

            dc = get_sbits(&gb, 9);

            dct_mode = get_bits1(&gb);

            class1 = get_bits(&gb, 2);

            if (DV_PROFILE_IS_HD(s->sys)) {

                mb->idct_put = s->idct_put[0];

                mb->scan_table = s->dv_zigzag[0];

                mb->factor_table = s->dv100_idct_factor[((s->sys->height == 720)<<1)|(j >= 4)][class1][quant];

                is_field_mode[mb_index] |= !j && dct_mode;

            } else {

                mb->idct_put = s->idct_put[dct_mode && log2_blocksize==3];

                mb->scan_table = s->dv_zigzag[dct_mode];

                mb->factor_table = s->dv_idct_factor[class1 == 3][dct_mode]

                    [quant + dv_quant_offset[class1]];

            }

            dc = dc << 2;

            /* convert to unsigned because 128 is not added in the

               standard IDCT */

            dc += 1024;

            block[0] = dc;

            buf_ptr += last_index >> 3;

            mb->pos = 0;

            mb->partial_bit_count = 0;

#ifdef VLC_DEBUG

            printf("MB block: %d, %d ", mb_index, j);

#endif

            dv_decode_ac(&gb, mb, block);

            /* write the remaining bits  in a new buffer only if the

               block is finished */

            if (mb->pos >= 64)

                bit_copy(&pb, &gb);

            block += 64;

            mb++;

        }

        /* pass 2 : we can do it just after */

#ifdef VLC_DEBUG

        printf("***pass 2 size=%d MB#=%d\n", put_bits_count(&pb), mb_index);

#endif

        block = block1;

        mb = mb1;

        init_get_bits(&gb, mb_bit_buffer, put_bits_count(&pb));

        flush_put_bits(&pb);

        for(j = 0;j < s->sys->bpm; j++, block += 64, mb++) {

            if (mb->pos < 64 && get_bits_left(&gb) > 0) {

                dv_decode_ac(&gb, mb, block);

                /* if still not finished, no need to parse other blocks */

                if (mb->pos < 64)

                    break;

            }

        }

        /* all blocks are finished, so the extra bytes can be used at

           the video segment level */

        if (j >= s->sys->bpm)

            bit_copy(&vs_pb, &gb);

    }

    /* we need a pass other the whole video segment */

#ifdef VLC_DEBUG

    printf("***pass 3 size=%d\n", put_bits_count(&vs_pb));

#endif

    block = &sblock[0][0];

    mb = mb_data;

    init_get_bits(&gb, vs_bit_buffer, put_bits_count(&vs_pb));

    flush_put_bits(&vs_pb);

    for(mb_index = 0; mb_index < 5; mb_index++) {

        for(j = 0;j < s->sys->bpm; j++) {

            if (mb->pos < 64) {

#ifdef VLC_DEBUG

                printf("start %d:%d\n", mb_index, j);

#endif

                dv_decode_ac(&gb, mb, block);

            }

            if (mb->pos >= 64 && mb->pos < 127)

                av_log(NULL, AV_LOG_ERROR, "AC EOB marker is absent pos=%d\n", mb->pos);

            block += 64;

            mb++;

        }

    }

    /* compute idct and place blocks */

    block = &sblock[0][0];

    mb = mb_data;

    for(mb_index = 0; mb_index < 5; mb_index++) {

        v = *mb_pos_ptr++;

        mb_x = v & 0xff;

        mb_y = v >> 8;

        /* We work with 720p frames split in half. The odd half-frame (chan==2,3) is displaced :-( */

        if (s->sys->height == 720 && !(s->buf[1]&0x0C)) {

               mb_y -= (mb_y>17)?18:-72; /* shifting the Y coordinate down by 72/2 macroblocks */

        }

        /* idct_put'ting luminance */

        if ((s->sys->pix_fmt == PIX_FMT_YUV420P) ||

            (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) ||

            (s->sys->height >= 720 && mb_y != 134)) {

            y_stride = (s->picture.linesize[0]<<((!is_field_mode[mb_index])*log2_blocksize)) - (2<<log2_blocksize);

        } else {

            y_stride = 0;

        }

        y_ptr = s->picture.data[0] + ((mb_y * s->picture.linesize[0] + mb_x)<<log2_blocksize);

        for(j = 0; j < 2; j++, y_ptr += y_stride) {

            for (i=0; i<2; i++, block += 64, mb++, y_ptr += (1<<log2_blocksize))

                 if (s->sys->pix_fmt == PIX_FMT_YUV422P && s->sys->width == 720 && i)

                     y_ptr -= (1<<log2_blocksize);

                 else

                     mb->idct_put(y_ptr, s->picture.linesize[0]<<is_field_mode[mb_index], block);

        }

        /* idct_put'ting chrominance */

        c_offset = (((mb_y>>(s->sys->pix_fmt == PIX_FMT_YUV420P)) * s->picture.linesize[1] +

                     (mb_x>>((s->sys->pix_fmt == PIX_FMT_YUV411P)?2:1)))<<log2_blocksize);

        for(j=2; j; j--) {

            uint8_t *c_ptr = s->picture.data[j] + c_offset;

            if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) {

                  uint64_t aligned_pixels[64/8];

                  uint8_t *pixels = (uint8_t*)aligned_pixels;

                  uint8_t *c_ptr1, *ptr1;

                  int x, y;

                  mb->idct_put(pixels, 8, block);

                  for(y = 0; y < (1<<log2_blocksize); y++, c_ptr += s->picture.linesize[j], pixels += 8) {

                      ptr1= pixels + (1<<(log2_blocksize-1));

                      c_ptr1 = c_ptr + (s->picture.linesize[j]<<log2_blocksize);

                      for(x=0; x < (1<<(log2_blocksize-1)); x++) {

                          c_ptr[x]= pixels[x];

                          c_ptr1[x]= ptr1[x];

                      }

                  }

                  block += 64; mb++;

            } else {

                  y_stride = (mb_y == 134) ? (1<<log2_blocksize) :

                                             s->picture.linesize[j]<<((!is_field_mode[mb_index])*log2_blocksize);

                  for (i=0; i<(1<<(s->sys->bpm==8)); i++, block += 64, mb++, c_ptr += y_stride)

                       mb->idct_put(c_ptr, s->picture.linesize[j]<<is_field_mode[mb_index], block);

            }

        }

    }

}


static bool adding_first_cpu(void)

{

    CPUState *cpu;

    size_t count = 0;

    CPU_FOREACH(cpu) {

        count++;

        if (count > 1) {

            return false;

        }

    }

    return true;

}


static void imc_get_coeffs(AVCodecContext *avctx,

                           IMCContext *q, IMCChannel *chctx)

{

    int i, j, cw_len, cw;

    for (i = 0; i < BANDS; i++) {

        if (!chctx->sumLenArr[i])

            continue;

        if (chctx->bandFlagsBuf[i] || chctx->bandWidthT[i]) {

            for (j = band_tab[i]; j < band_tab[i + 1]; j++) {

                cw_len = chctx->CWlengthT[j];

                cw = 0;

                if (cw_len && (!chctx->bandFlagsBuf[i] || !chctx->skipFlags[j])) {

                    if (get_bits_count(&q->gb) + cw_len > 512) {

                        av_log(avctx, AV_LOG_WARNING,

                            "Potential problem on band %i, coefficient %i"

                            ": cw_len=%i\n", i, j, cw_len);

                    }

                    cw = get_bits(&q->gb, cw_len);

                }

                chctx->codewords[j] = cw;

            }

        }

    }

}


static int aio_write_f(int argc, char **argv)

{

    int nr_iov, c;

    int pattern = 0xcd;

    struct aio_ctx *ctx = calloc(1, sizeof(struct aio_ctx));

    BlockDriverAIOCB *acb;

    while ((c = getopt(argc, argv, "CqP:")) != EOF) {

        switch (c) {

        case 'C':

            ctx->Cflag = 1;

            break;

        case 'q':

            ctx->qflag = 1;

            break;

        case 'P':

            pattern = parse_pattern(optarg);

            if (pattern < 0) {

                return 0;

            }

            break;

        default:

            return command_usage(&aio_write_cmd);

        }

    }

    if (optind > argc - 2) {

        return command_usage(&aio_write_cmd);

    }

    ctx->offset = cvtnum(argv[optind]);

    if (ctx->offset < 0) {

        printf("non-numeric length argument -- %s\n", argv[optind]);

        return 0;

    }

    optind++;

    if (ctx->offset & 0x1ff) {

        printf("offset %" PRId64 " is not sector aligned\n",

               ctx->offset);

        return 0;

    }

    nr_iov = argc - optind;

    ctx->buf = create_iovec(&ctx->qiov, &argv[optind], nr_iov, pattern);

    gettimeofday(&ctx->t1, NULL);

    acb = bdrv_aio_writev(bs, ctx->offset >> 9, &ctx->qiov,

                          ctx->qiov.size >> 9, aio_write_done, ctx);

    if (!acb) {

        free(ctx->buf);

        return -EIO;

    }

    return 0;

}


static coroutine_fn int quorum_co_flush(BlockDriverState *bs)

{

    BDRVQuorumState *s = bs->opaque;

    QuorumVoteVersion *winner = NULL;

    QuorumVotes error_votes;

    QuorumVoteValue result_value;

    int i;

    int result = 0;

    QLIST_INIT(&error_votes.vote_list);

    error_votes.compare = quorum_64bits_compare;

    for (i = 0; i < s->num_children; i++) {

        result = bdrv_co_flush(s->children[i]->bs);

        result_value.l = result;

        quorum_count_vote(&error_votes, &result_value, i);

    }

    winner = quorum_get_vote_winner(&error_votes);

    result = winner->value.l;

    quorum_free_vote_list(&error_votes);

    return result;

}


static void tcg_out_qemu_st(TCGContext *s, TCGReg data, TCGReg addr,

                            TCGMemOpIdx oi)

{

    TCGMemOp memop = get_memop(oi);

#ifdef CONFIG_SOFTMMU

    unsigned memi = get_mmuidx(oi);

    TCGReg addrz, param;

    tcg_insn_unit *func;

    tcg_insn_unit *label_ptr;

    addrz = tcg_out_tlb_load(s, addr, memi, memop & MO_SIZE,

                             offsetof(CPUTLBEntry, addr_write));

    /* The fast path is exactly one insn.  Thus we can perform the entire

       TLB Hit in the (annulled) delay slot of the branch over TLB Miss.  */

    /* beq,a,pt %[xi]cc, label0 */

    label_ptr = s->code_ptr;

    tcg_out_bpcc0(s, COND_E, BPCC_A | BPCC_PT

                  | (TARGET_LONG_BITS == 64 ? BPCC_XCC : BPCC_ICC), 0);

    /* delay slot */

    tcg_out_ldst_rr(s, data, addrz, TCG_REG_O1,

                    qemu_st_opc[memop & (MO_BSWAP | MO_SIZE)]);

    /* TLB Miss.  */

    param = TCG_REG_O1;

    if (!SPARC64 && TARGET_LONG_BITS == 64) {

        /* Skip the high-part; we'll perform the extract in the trampoline.  */

        param++;

    }

    tcg_out_mov(s, TCG_TYPE_REG, param++, addr);

    if (!SPARC64 && (memop & MO_SIZE) == MO_64) {

        /* Skip the high-part; we'll perform the extract in the trampoline.  */

        param++;

    }

    tcg_out_mov(s, TCG_TYPE_REG, param++, data);

    func = qemu_st_trampoline[memop & (MO_BSWAP | MO_SIZE)];

    assert(func != NULL);

    tcg_out_call_nodelay(s, func);

    /* delay slot */

    tcg_out_movi(s, TCG_TYPE_I32, param, oi);

    *label_ptr |= INSN_OFF19(tcg_ptr_byte_diff(s->code_ptr, label_ptr));

#else

    if (SPARC64 && TARGET_LONG_BITS == 32) {

        tcg_out_arithi(s, TCG_REG_T1, addr, 0, SHIFT_SRL);

        addr = TCG_REG_T1;

    }

    tcg_out_ldst_rr(s, data, addr,

                    (guest_base ? TCG_GUEST_BASE_REG : TCG_REG_G0),

                    qemu_st_opc[memop & (MO_BSWAP | MO_SIZE)]);

#endif /* CONFIG_SOFTMMU */

}


void r4k_helper_tlbr(CPUMIPSState *env)

{

    r4k_tlb_t *tlb;

    uint8_t ASID;

    int idx;

    ASID = env->CP0_EntryHi & 0xFF;

    idx = (env->CP0_Index & ~0x80000000) % env->tlb->nb_tlb;

    tlb = &env->tlb->mmu.r4k.tlb[idx];

    /* If this will change the current ASID, flush qemu's TLB.  */

    if (ASID != tlb->ASID)

        cpu_mips_tlb_flush (env, 1);

    r4k_mips_tlb_flush_extra(env, env->tlb->nb_tlb);

    env->CP0_EntryHi = tlb->VPN | tlb->ASID;

    env->CP0_PageMask = tlb->PageMask;

    env->CP0_EntryLo0 = tlb->G | (tlb->V0 << 1) | (tlb->D0 << 2) |

                        ((target_ulong)tlb->RI0 << CP0EnLo_RI) |

                        ((target_ulong)tlb->XI0 << CP0EnLo_XI) |

                        (tlb->C0 << 3) | (tlb->PFN[0] >> 6);

    env->CP0_EntryLo1 = tlb->G | (tlb->V1 << 1) | (tlb->D1 << 2) |

                        ((target_ulong)tlb->RI1 << CP0EnLo_RI) |

                        ((target_ulong)tlb->XI1 << CP0EnLo_XI) |

                        (tlb->C1 << 3) | (tlb->PFN[1] >> 6);

}


static int unpack_dct_coeffs(Vp3DecodeContext *s, GetBitContext *gb)

{

    int i;

    int dc_y_table;

    int dc_c_table;

    int ac_y_table;

    int ac_c_table;

    int residual_eob_run = 0;

    /* fetch the DC table indexes */

    dc_y_table = get_bits(gb, 4);

    dc_c_table = get_bits(gb, 4);

    /* unpack the Y plane DC coefficients */

    residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_y_table], 0,

        1, residual_eob_run);

    /* reverse prediction of the Y-plane DC coefficients */

    reverse_dc_prediction(s, 0, s->fragment_width, s->fragment_height);

    /* unpack the C plane DC coefficients */

    residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,

        0, residual_eob_run);

    /* reverse prediction of the C-plane DC coefficients */

    if (!(s->avctx->flags & CODEC_FLAG_GRAY))

    {

        reverse_dc_prediction(s, s->fragment_start[1],

            s->fragment_width / 2, s->fragment_height / 2);

        reverse_dc_prediction(s, s->fragment_start[2],

            s->fragment_width / 2, s->fragment_height / 2);

    }

    /* fetch the AC table indexes */

    ac_y_table = get_bits(gb, 4);

    ac_c_table = get_bits(gb, 4);

    /* unpack the group 1 AC coefficients (coeffs 1-5) */

    for (i = 1; i <= 5; i++) {

        residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_1[ac_y_table], i,

            1, residual_eob_run);

        residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_1[ac_c_table], i,

            0, residual_eob_run);

    }

    /* unpack the group 2 AC coefficients (coeffs 6-14) */

    for (i = 6; i <= 14; i++) {

        residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_2[ac_y_table], i,

            1, residual_eob_run);

        residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_2[ac_c_table], i,

            0, residual_eob_run);

    }

    /* unpack the group 3 AC coefficients (coeffs 15-27) */

    for (i = 15; i <= 27; i++) {

        residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_3[ac_y_table], i,

            1, residual_eob_run);

        residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_3[ac_c_table], i,

            0, residual_eob_run);

    }

    /* unpack the group 4 AC coefficients (coeffs 28-63) */

    for (i = 28; i <= 63; i++) {

        residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_4[ac_y_table], i,

            1, residual_eob_run);

        residual_eob_run = unpack_vlcs(s, gb, &s->ac_vlc_4[ac_c_table], i,

            0, residual_eob_run);

    }

    return 0;

}


int kvmppc_fixup_cpu(PowerPCCPU *cpu)

{

    CPUState *cs = CPU(cpu);

    int smt;

    /* Adjust cpu index for SMT */

    smt = kvmppc_smt_threads();

    cs->cpu_index = (cs->cpu_index / smp_threads) * smt

        + (cs->cpu_index % smp_threads);

    return 0;

}


static void *spapr_create_fdt_skel(const char *cpu_model,

                                   hwaddr initrd_base,

                                   hwaddr initrd_size,

                                   hwaddr kernel_size,

                                   const char *boot_device,

                                   const char *kernel_cmdline,

                                   uint32_t epow_irq)

{

    void *fdt;

    CPUPPCState *env;

    uint32_t start_prop = cpu_to_be32(initrd_base);

    uint32_t end_prop = cpu_to_be32(initrd_base + initrd_size);

    char hypertas_prop[] = "hcall-pft\0hcall-term\0hcall-dabr\0hcall-interrupt"

        "\0hcall-tce\0hcall-vio\0hcall-splpar\0hcall-bulk";

    char qemu_hypertas_prop[] = "hcall-memop1";

    uint32_t refpoints[] = {cpu_to_be32(0x4), cpu_to_be32(0x4)};

    uint32_t interrupt_server_ranges_prop[] = {0, cpu_to_be32(smp_cpus)};

    char *modelname;

    int i, smt = kvmppc_smt_threads();

    unsigned char vec5[] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x80};

    fdt = g_malloc0(FDT_MAX_SIZE);

    _FDT((fdt_create(fdt, FDT_MAX_SIZE)));

    if (kernel_size) {

        _FDT((fdt_add_reservemap_entry(fdt, KERNEL_LOAD_ADDR, kernel_size)));

    }

    if (initrd_size) {

        _FDT((fdt_add_reservemap_entry(fdt, initrd_base, initrd_size)));

    }

    _FDT((fdt_finish_reservemap(fdt)));

    /* Root node */

    _FDT((fdt_begin_node(fdt, "")));

    _FDT((fdt_property_string(fdt, "device_type", "chrp")));

    _FDT((fdt_property_string(fdt, "model", "IBM pSeries (emulated by qemu)")));

    _FDT((fdt_property_cell(fdt, "#address-cells", 0x2)));

    _FDT((fdt_property_cell(fdt, "#size-cells", 0x2)));

    /* /chosen */

    _FDT((fdt_begin_node(fdt, "chosen")));

    /* Set Form1_affinity */

    _FDT((fdt_property(fdt, "ibm,architecture-vec-5", vec5, sizeof(vec5))));

    _FDT((fdt_property_string(fdt, "bootargs", kernel_cmdline)));

    _FDT((fdt_property(fdt, "linux,initrd-start",

                       &start_prop, sizeof(start_prop))));

    _FDT((fdt_property(fdt, "linux,initrd-end",

                       &end_prop, sizeof(end_prop))));

    if (kernel_size) {

        uint64_t kprop[2] = { cpu_to_be64(KERNEL_LOAD_ADDR),

                              cpu_to_be64(kernel_size) };

        _FDT((fdt_property(fdt, "qemu,boot-kernel", &kprop, sizeof(kprop))));

    }

    _FDT((fdt_property_string(fdt, "qemu,boot-device", boot_device)));

    _FDT((fdt_property_cell(fdt, "qemu,graphic-width", graphic_width)));

    _FDT((fdt_property_cell(fdt, "qemu,graphic-height", graphic_height)));

    _FDT((fdt_property_cell(fdt, "qemu,graphic-depth", graphic_depth)));

    _FDT((fdt_end_node(fdt)));

    /* cpus */

    _FDT((fdt_begin_node(fdt, "cpus")));

    _FDT((fdt_property_cell(fdt, "#address-cells", 0x1)));

    _FDT((fdt_property_cell(fdt, "#size-cells", 0x0)));

    modelname = g_strdup(cpu_model);

    for (i = 0; i < strlen(modelname); i++) {

        modelname[i] = toupper(modelname[i]);

    }

    /* This is needed during FDT finalization */

    spapr->cpu_model = g_strdup(modelname);

    for (env = first_cpu; env != NULL; env = env->next_cpu) {

        CPUState *cpu = CPU(ppc_env_get_cpu(env));

        int index = cpu->cpu_index;

        uint32_t servers_prop[smp_threads];

        uint32_t gservers_prop[smp_threads * 2];

        char *nodename;

        uint32_t segs[] = {cpu_to_be32(28), cpu_to_be32(40),

                           0xffffffff, 0xffffffff};

        uint32_t tbfreq = kvm_enabled() ? kvmppc_get_tbfreq() : TIMEBASE_FREQ;

        uint32_t cpufreq = kvm_enabled() ? kvmppc_get_clockfreq() : 1000000000;

        uint32_t page_sizes_prop[64];

        size_t page_sizes_prop_size;

        if ((index % smt) != 0) {

            continue;

        }

        if (asprintf(&nodename, "%s@%x", modelname, index) < 0) {

            fprintf(stderr, "Allocation failure\n");

            exit(1);

        }

        _FDT((fdt_begin_node(fdt, nodename)));

        free(nodename);

        _FDT((fdt_property_cell(fdt, "reg", index)));

        _FDT((fdt_property_string(fdt, "device_type", "cpu")));

        _FDT((fdt_property_cell(fdt, "cpu-version", env->spr[SPR_PVR])));

        _FDT((fdt_property_cell(fdt, "dcache-block-size",

                                env->dcache_line_size)));

        _FDT((fdt_property_cell(fdt, "icache-block-size",

                                env->icache_line_size)));

        _FDT((fdt_property_cell(fdt, "timebase-frequency", tbfreq)));

        _FDT((fdt_property_cell(fdt, "clock-frequency", cpufreq)));

        _FDT((fdt_property_cell(fdt, "ibm,slb-size", env->slb_nr)));

        _FDT((fdt_property_string(fdt, "status", "okay")));

        _FDT((fdt_property(fdt, "64-bit", NULL, 0)));

        /* Build interrupt servers and gservers properties */

        for (i = 0; i < smp_threads; i++) {

            servers_prop[i] = cpu_to_be32(index + i);

            /* Hack, direct the group queues back to cpu 0 */

            gservers_prop[i*2] = cpu_to_be32(index + i);

            gservers_prop[i*2 + 1] = 0;

        }

        _FDT((fdt_property(fdt, "ibm,ppc-interrupt-server#s",

                           servers_prop, sizeof(servers_prop))));

        _FDT((fdt_property(fdt, "ibm,ppc-interrupt-gserver#s",

                           gservers_prop, sizeof(gservers_prop))));

        if (env->mmu_model & POWERPC_MMU_1TSEG) {

            _FDT((fdt_property(fdt, "ibm,processor-segment-sizes",

                               segs, sizeof(segs))));

        }

        /* Advertise VMX/VSX (vector extensions) if available

         *   0 / no property == no vector extensions

         *   1               == VMX / Altivec available

         *   2               == VSX available */

        if (env->insns_flags & PPC_ALTIVEC) {

            uint32_t vmx = (env->insns_flags2 & PPC2_VSX) ? 2 : 1;

            _FDT((fdt_property_cell(fdt, "ibm,vmx", vmx)));

        }

        /* Advertise DFP (Decimal Floating Point) if available

         *   0 / no property == no DFP

         *   1               == DFP available */

        if (env->insns_flags2 & PPC2_DFP) {

            _FDT((fdt_property_cell(fdt, "ibm,dfp", 1)));

        }

        page_sizes_prop_size = create_page_sizes_prop(env, page_sizes_prop,

                                                      sizeof(page_sizes_prop));

        if (page_sizes_prop_size) {

            _FDT((fdt_property(fdt, "ibm,segment-page-sizes",

                               page_sizes_prop, page_sizes_prop_size)));

        }

        _FDT((fdt_end_node(fdt)));

    }

    g_free(modelname);

    _FDT((fdt_end_node(fdt)));

    /* RTAS */

    _FDT((fdt_begin_node(fdt, "rtas")));

    _FDT((fdt_property(fdt, "ibm,hypertas-functions", hypertas_prop,

                       sizeof(hypertas_prop))));

    _FDT((fdt_property(fdt, "qemu,hypertas-functions", qemu_hypertas_prop,

                       sizeof(qemu_hypertas_prop))));

    _FDT((fdt_property(fdt, "ibm,associativity-reference-points",

        refpoints, sizeof(refpoints))));

    _FDT((fdt_property_cell(fdt, "rtas-error-log-max", RTAS_ERROR_LOG_MAX)));

    _FDT((fdt_end_node(fdt)));

    /* interrupt controller */

    _FDT((fdt_begin_node(fdt, "interrupt-controller")));

    _FDT((fdt_property_string(fdt, "device_type",

                              "PowerPC-External-Interrupt-Presentation")));

    _FDT((fdt_property_string(fdt, "compatible", "IBM,ppc-xicp")));

    _FDT((fdt_property(fdt, "interrupt-controller", NULL, 0)));

    _FDT((fdt_property(fdt, "ibm,interrupt-server-ranges",

                       interrupt_server_ranges_prop,

                       sizeof(interrupt_server_ranges_prop))));

    _FDT((fdt_property_cell(fdt, "#interrupt-cells", 2)));

    _FDT((fdt_property_cell(fdt, "linux,phandle", PHANDLE_XICP)));

    _FDT((fdt_property_cell(fdt, "phandle", PHANDLE_XICP)));

    _FDT((fdt_end_node(fdt)));

    /* vdevice */

    _FDT((fdt_begin_node(fdt, "vdevice")));

    _FDT((fdt_property_string(fdt, "device_type", "vdevice")));

    _FDT((fdt_property_string(fdt, "compatible", "IBM,vdevice")));

    _FDT((fdt_property_cell(fdt, "#address-cells", 0x1)));

    _FDT((fdt_property_cell(fdt, "#size-cells", 0x0)));

    _FDT((fdt_property_cell(fdt, "#interrupt-cells", 0x2)));

    _FDT((fdt_property(fdt, "interrupt-controller", NULL, 0)));

    _FDT((fdt_end_node(fdt)));

    /* event-sources */

    spapr_events_fdt_skel(fdt, epow_irq);

    _FDT((fdt_end_node(fdt))); /* close root node */

    _FDT((fdt_finish(fdt)));

    return fdt;

}


static int create_header64(DumpState *s)

{

    int ret = 0;

    DiskDumpHeader64 *dh = NULL;

    KdumpSubHeader64 *kh = NULL;

    size_t size;

    int endian = s->dump_info.d_endian;

    uint32_t block_size;

    uint32_t sub_hdr_size;

    uint32_t bitmap_blocks;

    uint32_t status = 0;

    uint64_t offset_note;

    /* write common header, the version of kdump-compressed format is 6th */

    size = sizeof(DiskDumpHeader64);

    dh = g_malloc0(size);

    strncpy(dh->signature, KDUMP_SIGNATURE, strlen(KDUMP_SIGNATURE));

    dh->header_version = cpu_convert_to_target32(6, endian);

    block_size = s->page_size;

    dh->block_size = cpu_convert_to_target32(block_size, endian);

    sub_hdr_size = sizeof(struct KdumpSubHeader64) + s->note_size;

    sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);

    dh->sub_hdr_size = cpu_convert_to_target32(sub_hdr_size, endian);

    /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */

    dh->max_mapnr = cpu_convert_to_target32(MIN(s->max_mapnr, UINT_MAX),

                                            endian);

    dh->nr_cpus = cpu_convert_to_target32(s->nr_cpus, endian);

    bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;

    dh->bitmap_blocks = cpu_convert_to_target32(bitmap_blocks, endian);

    strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));

    if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {

        status |= DUMP_DH_COMPRESSED_ZLIB;

    }

#ifdef CONFIG_LZO

    if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {

        status |= DUMP_DH_COMPRESSED_LZO;

    }

#endif

#ifdef CONFIG_SNAPPY

    if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {

        status |= DUMP_DH_COMPRESSED_SNAPPY;

    }

#endif

    dh->status = cpu_convert_to_target32(status, endian);

    if (write_buffer(s->fd, 0, dh, size) < 0) {

        dump_error(s, "dump: failed to write disk dump header.\n");

        ret = -1;

        goto out;

    }

    /* write sub header */

    size = sizeof(KdumpSubHeader64);

    kh = g_malloc0(size);

    /* 64bit max_mapnr_64 */

    kh->max_mapnr_64 = cpu_convert_to_target64(s->max_mapnr, endian);

    kh->phys_base = cpu_convert_to_target64(PHYS_BASE, endian);

    kh->dump_level = cpu_convert_to_target32(DUMP_LEVEL, endian);

    offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;

    kh->offset_note = cpu_convert_to_target64(offset_note, endian);

    kh->note_size = cpu_convert_to_target64(s->note_size, endian);

    if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS *

                     block_size, kh, size) < 0) {

        dump_error(s, "dump: failed to write kdump sub header.\n");

        ret = -1;

        goto out;

    }

    /* write note */

    s->note_buf = g_malloc0(s->note_size);

    s->note_buf_offset = 0;

    /* use s->note_buf to store notes temporarily */

    if (write_elf64_notes(buf_write_note, s) < 0) {

        ret = -1;

        goto out;

    }

    if (write_buffer(s->fd, offset_note, s->note_buf,

                     s->note_size) < 0) {

        dump_error(s, "dump: failed to write notes");

        ret = -1;

        goto out;

    }

    /* get offset of dump_bitmap */

    s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *

                             block_size;

    /* get offset of page */

    s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *

                     block_size;

out:

    g_free(dh);

    g_free(kh);

    g_free(s->note_buf);

    return ret;

}


unsigned int SingleCPDO(const unsigned int opcode)

{

   FPA11 *fpa11 = GET_FPA11();

   float32 rFm, rFn = 0;

   unsigned int Fd, Fm, Fn, nRc = 1;

   Fm = getFm(opcode);

   if (CONSTANT_FM(opcode))

   {

     rFm = getSingleConstant(Fm);

   }

   else

   {

     switch (fpa11->fType[Fm])

     {

        case typeSingle:

          rFm = fpa11->fpreg[Fm].fSingle;

        break;

        default: return 0;

     }

   }

   if (!MONADIC_INSTRUCTION(opcode))

   {

      Fn = getFn(opcode);

      switch (fpa11->fType[Fn])

      {

        case typeSingle:

          rFn = fpa11->fpreg[Fn].fSingle;

        break;

        default: return 0;

      }

   }

   Fd = getFd(opcode);

   switch (opcode & MASK_ARITHMETIC_OPCODE)

   {

      /* dyadic opcodes */

      case ADF_CODE:

         fpa11->fpreg[Fd].fSingle = float32_add(rFn,rFm, &fpa11->fp_status);

      break;

      case MUF_CODE:

      case FML_CODE:

        fpa11->fpreg[Fd].fSingle = float32_mul(rFn,rFm, &fpa11->fp_status);

      break;

      case SUF_CODE:

         fpa11->fpreg[Fd].fSingle = float32_sub(rFn,rFm, &fpa11->fp_status);

      break;

      case RSF_CODE:

         fpa11->fpreg[Fd].fSingle = float32_sub(rFm,rFn, &fpa11->fp_status);

      break;

      case DVF_CODE:

      case FDV_CODE:

         fpa11->fpreg[Fd].fSingle = float32_div(rFn,rFm, &fpa11->fp_status);

      break;

      case RDF_CODE:

      case FRD_CODE:

         fpa11->fpreg[Fd].fSingle = float32_div(rFm,rFn, &fpa11->fp_status);

      break;

#if 0

      case POW_CODE:

         fpa11->fpreg[Fd].fSingle = float32_pow(rFn,rFm);

      break;

      case RPW_CODE:

         fpa11->fpreg[Fd].fSingle = float32_pow(rFm,rFn);

      break;

#endif

      case RMF_CODE:

         fpa11->fpreg[Fd].fSingle = float32_rem(rFn,rFm, &fpa11->fp_status);

      break;

#if 0

      case POL_CODE:

         fpa11->fpreg[Fd].fSingle = float32_pol(rFn,rFm);

      break;

#endif

      /* monadic opcodes */

      case MVF_CODE:

         fpa11->fpreg[Fd].fSingle = rFm;

      break;

      case MNF_CODE:

         rFm ^= 0x80000000;

         fpa11->fpreg[Fd].fSingle = rFm;

      break;

      case ABS_CODE:

         rFm &= 0x7fffffff;

         fpa11->fpreg[Fd].fSingle = rFm;

      break;

      case RND_CODE:

      case URD_CODE:

         fpa11->fpreg[Fd].fSingle = float32_round_to_int(rFm, &fpa11->fp_status);

      break;

      case SQT_CODE:

         fpa11->fpreg[Fd].fSingle = float32_sqrt(rFm, &fpa11->fp_status);

      break;

#if 0

      case LOG_CODE:

         fpa11->fpreg[Fd].fSingle = float32_log(rFm);

      break;

      case LGN_CODE:

         fpa11->fpreg[Fd].fSingle = float32_ln(rFm);

      break;

      case EXP_CODE:

         fpa11->fpreg[Fd].fSingle = float32_exp(rFm);

      break;

      case SIN_CODE:

         fpa11->fpreg[Fd].fSingle = float32_sin(rFm);

      break;

      case COS_CODE:

         fpa11->fpreg[Fd].fSingle = float32_cos(rFm);

      break;

      case TAN_CODE:

         fpa11->fpreg[Fd].fSingle = float32_tan(rFm);

      break;

      case ASN_CODE:

         fpa11->fpreg[Fd].fSingle = float32_arcsin(rFm);

      break;

      case ACS_CODE:

         fpa11->fpreg[Fd].fSingle = float32_arccos(rFm);

      break;

      case ATN_CODE:

         fpa11->fpreg[Fd].fSingle = float32_arctan(rFm);

      break;

#endif

      case NRM_CODE:

      break;

      default:

      {

        nRc = 0;

      }

   }

   if (0 != nRc) fpa11->fType[Fd] = typeSingle;

   return nRc;

}


static void mirror_start_job(BlockDriverState *bs, BlockDriverState *target,

                             const char *replaces,

                             int64_t speed, uint32_t granularity,

                             int64_t buf_size,

                             BlockdevOnError on_source_error,

                             BlockdevOnError on_target_error,

                             BlockCompletionFunc *cb,

                             void *opaque, Error **errp,

                             const BlockJobDriver *driver,

                             bool is_none_mode, BlockDriverState *base)

{

    MirrorBlockJob *s;

    if (granularity == 0) {

        /* Choose the default granularity based on the target file's cluster

         * size, clamped between 4k and 64k.  */

        BlockDriverInfo bdi;

        if (bdrv_get_info(target, &bdi) >= 0 && bdi.cluster_size != 0) {

            granularity = MAX(4096, bdi.cluster_size);

            granularity = MIN(65536, granularity);

        } else {

            granularity = 65536;

        }

    }

    assert ((granularity & (granularity - 1)) == 0);

    if ((on_source_error == BLOCKDEV_ON_ERROR_STOP ||

         on_source_error == BLOCKDEV_ON_ERROR_ENOSPC) &&

        !bdrv_iostatus_is_enabled(bs)) {

        error_set(errp, QERR_INVALID_PARAMETER, "on-source-error");

        return;

    }

    s = block_job_create(driver, bs, speed, cb, opaque, errp);

    if (!s) {

        return;

    }

    s->replaces = g_strdup(replaces);

    s->on_source_error = on_source_error;

    s->on_target_error = on_target_error;

    s->target = target;

    s->is_none_mode = is_none_mode;

    s->base = base;

    s->granularity = granularity;

    s->buf_size = MAX(buf_size, granularity);

    s->dirty_bitmap = bdrv_create_dirty_bitmap(bs, granularity, NULL, errp);

    if (!s->dirty_bitmap) {

        return;

    }

    bdrv_set_enable_write_cache(s->target, true);

    bdrv_set_on_error(s->target, on_target_error, on_target_error);

    bdrv_iostatus_enable(s->target);

    s->common.co = qemu_coroutine_create(mirror_run);

    trace_mirror_start(bs, s, s->common.co, opaque);

    qemu_coroutine_enter(s->common.co, s);

}


static int ac3_decode_frame(AVCodecContext * avctx, void *data, int *data_size,

                            AVPacket *avpkt)

{

    const uint8_t *buf = avpkt->data;

    int buf_size = avpkt->size;

    AC3DecodeContext *s = avctx->priv_data;

    int16_t *out_samples = (int16_t *)data;

    int blk, ch, err;

    const uint8_t *channel_map;

    const float *output[AC3_MAX_CHANNELS];

    /* initialize the GetBitContext with the start of valid AC-3 Frame */

    if (s->input_buffer) {

        /* copy input buffer to decoder context to avoid reading past the end

           of the buffer, which can be caused by a damaged input stream. */

        memcpy(s->input_buffer, buf, FFMIN(buf_size, AC3_FRAME_BUFFER_SIZE));

        init_get_bits(&s->gbc, s->input_buffer, buf_size * 8);

    } else {

        init_get_bits(&s->gbc, buf, buf_size * 8);

    }

    /* parse the syncinfo */

    *data_size = 0;

    err = parse_frame_header(s);

    if (err) {

        switch(err) {

            case AAC_AC3_PARSE_ERROR_SYNC:

                av_log(avctx, AV_LOG_ERROR, "frame sync error\n");

                return -1;

            case AAC_AC3_PARSE_ERROR_BSID:

                av_log(avctx, AV_LOG_ERROR, "invalid bitstream id\n");

                break;

            case AAC_AC3_PARSE_ERROR_SAMPLE_RATE:

                av_log(avctx, AV_LOG_ERROR, "invalid sample rate\n");

                break;

            case AAC_AC3_PARSE_ERROR_FRAME_SIZE:

                av_log(avctx, AV_LOG_ERROR, "invalid frame size\n");

                break;

            case AAC_AC3_PARSE_ERROR_FRAME_TYPE:

                /* skip frame if CRC is ok. otherwise use error concealment. */

                /* TODO: add support for substreams and dependent frames */

                if(s->frame_type == EAC3_FRAME_TYPE_DEPENDENT || s->substreamid) {

                    av_log(avctx, AV_LOG_ERROR, "unsupported frame type : skipping frame\n");

                    return s->frame_size;

                } else {

                    av_log(avctx, AV_LOG_ERROR, "invalid frame type\n");

                }

                break;

            default:

                av_log(avctx, AV_LOG_ERROR, "invalid header\n");

                break;

        }

    } else {

        /* check that reported frame size fits in input buffer */

        if (s->frame_size > buf_size) {

            av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");

            err = AAC_AC3_PARSE_ERROR_FRAME_SIZE;

        } else if (avctx->error_recognition >= FF_ER_CAREFUL) {

            /* check for crc mismatch */

            if (av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, &buf[2], s->frame_size-2)) {

                av_log(avctx, AV_LOG_ERROR, "frame CRC mismatch\n");

                err = AAC_AC3_PARSE_ERROR_CRC;

            }

        }

    }

    /* if frame is ok, set audio parameters */

    if (!err) {

        avctx->sample_rate = s->sample_rate;

        avctx->bit_rate = s->bit_rate;

        /* channel config */

        s->out_channels = s->channels;

        s->output_mode = s->channel_mode;

        if(s->lfe_on)

            s->output_mode |= AC3_OUTPUT_LFEON;

        if (avctx->request_channels > 0 && avctx->request_channels <= 2 &&

                avctx->request_channels < s->channels) {

            s->out_channels = avctx->request_channels;

            s->output_mode  = avctx->request_channels == 1 ? AC3_CHMODE_MONO : AC3_CHMODE_STEREO;

            s->channel_layout = ff_ac3_channel_layout_tab[s->output_mode];

        }

        avctx->channels = s->out_channels;

        avctx->channel_layout = s->channel_layout;

        /* set downmixing coefficients if needed */

        if(s->channels != s->out_channels && !((s->output_mode & AC3_OUTPUT_LFEON) &&

                s->fbw_channels == s->out_channels)) {

            set_downmix_coeffs(s);

        }

    } else if (!s->out_channels) {

        s->out_channels = avctx->channels;

        if(s->out_channels < s->channels)

            s->output_mode  = s->out_channels == 1 ? AC3_CHMODE_MONO : AC3_CHMODE_STEREO;

    }

    /* decode the audio blocks */

    channel_map = ff_ac3_dec_channel_map[s->output_mode & ~AC3_OUTPUT_LFEON][s->lfe_on];

    for (ch = 0; ch < s->out_channels; ch++)

        output[ch] = s->output[channel_map[ch]];

    for (blk = 0; blk < s->num_blocks; blk++) {

        if (!err && decode_audio_block(s, blk)) {

            av_log(avctx, AV_LOG_ERROR, "error decoding the audio block\n");

            err = 1;

        }

        s->fmt_conv.float_to_int16_interleave(out_samples, output, 256, s->out_channels);

        out_samples += 256 * s->out_channels;

    }

    *data_size = s->num_blocks * 256 * avctx->channels * sizeof (int16_t);

    return FFMIN(buf_size, s->frame_size);

}


static void v9fs_flush(void *opaque)

{

    int16_t tag;

    size_t offset = 7;

    V9fsPDU *cancel_pdu;

    V9fsPDU *pdu = opaque;

    V9fsState *s = pdu->s;

    pdu_unmarshal(pdu, offset, "w", &tag);

    QLIST_FOREACH(cancel_pdu, &s->active_list, next) {

        if (cancel_pdu->tag == tag) {

            break;

        }

    }

    if (cancel_pdu) {

        cancel_pdu->cancelled = 1;

        /*

         * Wait for pdu to complete.

         */

        qemu_co_queue_wait(&cancel_pdu->complete);

        cancel_pdu->cancelled = 0;

        free_pdu(pdu->s, cancel_pdu);

    }

    complete_pdu(s, pdu, 7);

    return;

}


void qemu_savevm_send_postcopy_advise(QEMUFile *f)

{

    uint64_t tmp[2];

    tmp[0] = cpu_to_be64(getpagesize());

    tmp[1] = cpu_to_be64(1ul << qemu_target_page_bits());

    trace_qemu_savevm_send_postcopy_advise();

    qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_ADVISE, 16, (uint8_t *)tmp);

}


static void add_qemu_cpu_model_features(S390FeatBitmap fbm)

{

    static const int feats[] = {

        S390_FEAT_DAT_ENH,

        S390_FEAT_IDTE_SEGMENT,

        S390_FEAT_STFLE,

        S390_FEAT_SENSE_RUNNING_STATUS,

        S390_FEAT_EXTENDED_IMMEDIATE,

        S390_FEAT_EXTENDED_TRANSLATION_2,

        S390_FEAT_MSA,

        S390_FEAT_EXTENDED_TRANSLATION_3,

        S390_FEAT_LONG_DISPLACEMENT,

        S390_FEAT_LONG_DISPLACEMENT_FAST,

        S390_FEAT_ETF2_ENH,

        S390_FEAT_STORE_CLOCK_FAST,

        S390_FEAT_MOVE_WITH_OPTIONAL_SPEC,

        S390_FEAT_ETF3_ENH,

        S390_FEAT_COMPARE_AND_SWAP_AND_STORE,

        S390_FEAT_COMPARE_AND_SWAP_AND_STORE_2,

        S390_FEAT_GENERAL_INSTRUCTIONS_EXT,

        S390_FEAT_EXECUTE_EXT,

        S390_FEAT_FLOATING_POINT_SUPPPORT_ENH,

        S390_FEAT_STFLE_45,

        S390_FEAT_STFLE_49,

        S390_FEAT_LOCAL_TLB_CLEARING,

        S390_FEAT_INTERLOCKED_ACCESS_2,

        S390_FEAT_STFLE_53,

        S390_FEAT_MSA_EXT_5,

        S390_FEAT_MSA_EXT_3,

        S390_FEAT_MSA_EXT_4,

    };

    int i;

    for (i = 0; i < ARRAY_SIZE(feats); i++) {

        set_bit(feats[i], fbm);

    }

}


static target_long monitor_get_decr (const struct MonitorDef *md, int val)

{

    CPUState *env = mon_get_cpu();

    if (!env)

        return 0;

    return cpu_ppc_load_decr(env);

}


START_TEST(qobject_to_qlist_test)

{

    QList *qlist;

    qlist = qlist_new();

    fail_unless(qobject_to_qlist(QOBJECT(qlist)) == qlist);

    // destroy doesn't exist yet

    g_free(qlist);

}


CharDriverState *qemu_chr_find(const char *name)

{

    CharDriverState *chr;

    TAILQ_FOREACH(chr, &chardevs, next) {

        if (strcmp(chr->label, name) != 0)

            continue;

        return chr;

    }

    return NULL;

}


static int a64multi_encode_frame(AVCodecContext *avctx, AVPacket *pkt,

                                 const AVFrame *pict, int *got_packet)

{

    A64Context *c = avctx->priv_data;

    AVFrame *const p = avctx->coded_frame;

    int frame;

    int x, y;

    int b_height;

    int b_width;

    int req_size, ret;

    uint8_t *buf;

    int *charmap     = c->mc_charmap;

    uint8_t *colram  = c->mc_colram;

    uint8_t *charset = c->mc_charset;

    int *meta        = c->mc_meta_charset;

    int *best_cb     = c->mc_best_cb;

    int charset_size = 0x800 * (INTERLACED + 1);

    int colram_size  = 0x100 * c->mc_use_5col;

    int screen_size;

    if(CROP_SCREENS) {

        b_height = FFMIN(avctx->height,C64YRES) >> 3;

        b_width  = FFMIN(avctx->width ,C64XRES) >> 3;

        screen_size = b_width * b_height;

    } else {

        b_height = C64YRES >> 3;

        b_width  = C64XRES >> 3;

        screen_size = 0x400;

    }

    /* no data, means end encoding asap */

    if (!pict) {

        /* all done, end encoding */

        if (!c->mc_lifetime) return 0;

        /* no more frames in queue, prepare to flush remaining frames */

        if (!c->mc_frame_counter) {

            c->mc_lifetime = 0;

        }

        /* still frames in queue so limit lifetime to remaining frames */

        else c->mc_lifetime = c->mc_frame_counter;

    /* still new data available */

    } else {

        /* fill up mc_meta_charset with data until lifetime exceeds */

        if (c->mc_frame_counter < c->mc_lifetime) {

            *p = *pict;

            p->pict_type = AV_PICTURE_TYPE_I;

            p->key_frame = 1;

            to_meta_with_crop(avctx, p, meta + 32000 * c->mc_frame_counter);

            c->mc_frame_counter++;

            if (c->next_pts == AV_NOPTS_VALUE)

                c->next_pts = pict->pts;

            /* lifetime is not reached so wait for next frame first */

            return 0;

        }

    }

    /* lifetime reached so now convert X frames at once */

    if (c->mc_frame_counter == c->mc_lifetime) {

        req_size = 0;

        /* any frames to encode? */

        if (c->mc_lifetime) {

            req_size = charset_size + c->mc_lifetime*(screen_size + colram_size);

            if ((ret = ff_alloc_packet(pkt, req_size)) < 0) {

                av_log(avctx, AV_LOG_ERROR, "Error getting output packet of size %d.\n", req_size);

                return ret;

            }

            buf = pkt->data;

            /* calc optimal new charset + charmaps */

            ret = ff_init_elbg(meta, 32, 1000 * c->mc_lifetime, best_cb,

                               CHARSET_CHARS, 50, charmap, &c->randctx);

            if (ret < 0)

                return ret;

            ret = ff_do_elbg(meta, 32, 1000 * c->mc_lifetime, best_cb,

                             CHARSET_CHARS, 50, charmap, &c->randctx);

            if (ret < 0)

                return ret;

            /* create colorram map and a c64 readable charset */

            render_charset(avctx, charset, colram);

            /* copy charset to buf */

            memcpy(buf, charset, charset_size);

            /* advance pointers */

            buf      += charset_size;

            charset  += charset_size;

        }

        /* write x frames to buf */

        for (frame = 0; frame < c->mc_lifetime; frame++) {

            /* copy charmap to buf. buf is uchar*, charmap is int*, so no memcpy here, sorry */

            for (y = 0; y < b_height; y++) {

                for (x = 0; x < b_width; x++) {

                    buf[y * b_width + x] = charmap[y * b_width + x];

                }

            }

            /* advance pointers */

            buf += screen_size;

            req_size += screen_size;

            /* compress and copy colram to buf */

            if (c->mc_use_5col) {

                a64_compress_colram(buf, charmap, colram);

                /* advance pointers */

                buf += colram_size;

                req_size += colram_size;

            }

            /* advance to next charmap */

            charmap += 1000;

        }

        AV_WB32(avctx->extradata + 4,  c->mc_frame_counter);

        AV_WB32(avctx->extradata + 8,  charset_size);

        AV_WB32(avctx->extradata + 12, screen_size + colram_size);

        /* reset counter */

        c->mc_frame_counter = 0;

        pkt->pts = pkt->dts = c->next_pts;

        c->next_pts         = AV_NOPTS_VALUE;

        pkt->size   = req_size;

        pkt->flags |= AV_PKT_FLAG_KEY;

        *got_packet = !!req_size;

    }

    return 0;

}


static void vm_completion(ReadLineState *rs, const char *str)

{

    size_t len;

    BlockDriverState *bs;

    BdrvNextIterator *it = NULL;

    len = strlen(str);

    readline_set_completion_index(rs, len);

    while ((it = bdrv_next(it, &bs))) {

        SnapshotInfoList *snapshots, *snapshot;

        AioContext *ctx = bdrv_get_aio_context(bs);

        bool ok = false;

        aio_context_acquire(ctx);

        if (bdrv_can_snapshot(bs)) {

            ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;

        }

        aio_context_release(ctx);

        if (!ok) {

            continue;

        }

        snapshot = snapshots;

        while (snapshot) {

            char *completion = snapshot->value->name;

            if (!strncmp(str, completion, len)) {

                readline_add_completion(rs, completion);

            }

            completion = snapshot->value->id;

            if (!strncmp(str, completion, len)) {

                readline_add_completion(rs, completion);

            }

            snapshot = snapshot->next;

        }

        qapi_free_SnapshotInfoList(snapshots);

    }

}


static int mpegps_read_packet(AVFormatContext *s,

                              AVPacket *pkt)

{

    AVStream *st;

    int len, startcode, i, type, codec_id;

    int64_t pts, dts;

 redo:

    len = mpegps_read_pes_header(s, NULL, &startcode, &pts, &dts, 1);

    if (len < 0)

        return len;

    /* now find stream */

    for(i=0;i<s->nb_streams;i++) {

        st = s->streams[i];

        if (st->id == startcode)

            goto found;

    }

    if (startcode >= 0x1e0 && startcode <= 0x1ef) {

        type = CODEC_TYPE_VIDEO;

        codec_id = CODEC_ID_MPEG1VIDEO;

    } else if (startcode >= 0x1c0 && startcode <= 0x1df) {

        type = CODEC_TYPE_AUDIO;

        codec_id = CODEC_ID_MP2;

    } else if (startcode >= 0x80 && startcode <= 0x9f) {

        type = CODEC_TYPE_AUDIO;

        codec_id = CODEC_ID_AC3;

    } else if (startcode >= 0xa0 && startcode <= 0xbf) {

        type = CODEC_TYPE_AUDIO;

        codec_id = CODEC_ID_PCM_S16BE;

    } else {

    skip:

        /* skip packet */

        url_fskip(&s->pb, len);

        goto redo;

    }

    /* no stream found: add a new stream */

    st = av_new_stream(s, startcode);

    if (!st)

        goto skip;

    st->codec.codec_type = type;

    st->codec.codec_id = codec_id;

    if (codec_id != CODEC_ID_PCM_S16BE)

        st->need_parsing = 1;

 found:

    if (startcode >= 0xa0 && startcode <= 0xbf) {

        int b1, freq;

        static const int lpcm_freq_tab[4] = { 48000, 96000, 44100, 32000 };

        /* for LPCM, we just skip the header and consider it is raw

           audio data */

        if (len <= 3)

            goto skip;

        get_byte(&s->pb); /* emphasis (1), muse(1), reserved(1), frame number(5) */

        b1 = get_byte(&s->pb); /* quant (2), freq(2), reserved(1), channels(3) */

        get_byte(&s->pb); /* dynamic range control (0x80 = off) */

        len -= 3;

        freq = (b1 >> 4) & 3;

        st->codec.sample_rate = lpcm_freq_tab[freq];

        st->codec.channels = 1 + (b1 & 7);

        st->codec.bit_rate = st->codec.channels * st->codec.sample_rate * 2;

    }

    av_new_packet(pkt, len);

    get_buffer(&s->pb, pkt->data, pkt->size);

    pkt->pts = pts;

    pkt->dts = dts;

    pkt->stream_index = st->index;

#if 0

    printf("%d: pts=%0.3f dts=%0.3f\n",

           pkt->stream_index, pkt->pts / 90000.0, pkt->dts / 90000.0);

#endif

    return 0;

}


static int check_for_evdev(void)

{

    SDL_SysWMinfo info;

    XkbDescPtr desc;

    int has_evdev = 0;

    const char *keycodes;

    SDL_VERSION(&info.version);

    if (!SDL_GetWMInfo(&info))

        return 0;

    desc = XkbGetKeyboard(info.info.x11.display,

                          XkbGBN_AllComponentsMask,

                          XkbUseCoreKbd);

    if (desc == NULL || desc->names == NULL)

        return 0;

    keycodes = XGetAtomName(info.info.x11.display, desc->names->keycodes);

    if (keycodes == NULL)

        fprintf(stderr, "could not lookup keycode name\n");

    else if (strstart(keycodes, "evdev", NULL))

        has_evdev = 1;

    else if (!strstart(keycodes, "xfree86", NULL))

        fprintf(stderr,

                "unknown keycodes `%s', please report to qemu-devel@nongnu.org\n",

                keycodes);

    XkbFreeClientMap(desc, XkbGBN_AllComponentsMask, True);

    return has_evdev;

}


static MemTxResult memory_region_oldmmio_read_accessor(MemoryRegion *mr,

                                                       hwaddr addr,

                                                       uint64_t *value,

                                                       unsigned size,

                                                       unsigned shift,

                                                       uint64_t mask,

                                                       MemTxAttrs attrs)

{

    uint64_t tmp;

    tmp = mr->ops->old_mmio.read[ctz32(size)](mr->opaque, addr);

    if (mr->subpage) {

        trace_memory_region_subpage_read(get_cpu_index(), mr, addr, tmp, size);

    } else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED) {

        hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr);

        trace_memory_region_ops_read(get_cpu_index(), mr, abs_addr, tmp, size);

    }

    *value |= (tmp & mask) << shift;

    return MEMTX_OK;

}