qemu/chardev/char-mux.c
<<
>>
Prefs
   1/*
   2 * QEMU System Emulator
   3 *
   4 * Copyright (c) 2003-2008 Fabrice Bellard
   5 *
   6 * Permission is hereby granted, free of charge, to any person obtaining a copy
   7 * of this software and associated documentation files (the "Software"), to deal
   8 * in the Software without restriction, including without limitation the rights
   9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10 * copies of the Software, and to permit persons to whom the Software is
  11 * furnished to do so, subject to the following conditions:
  12 *
  13 * The above copyright notice and this permission notice shall be included in
  14 * all copies or substantial portions of the Software.
  15 *
  16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22 * THE SOFTWARE.
  23 */
  24#include "qemu/osdep.h"
  25#include "qapi/error.h"
  26#include "qemu-common.h"
  27#include "chardev/char.h"
  28#include "sysemu/block-backend.h"
  29#include "chardev/char-mux.h"
  30
  31/* MUX driver for serial I/O splitting */
  32
  33/* Called with chr_write_lock held.  */
  34static int mux_chr_write(Chardev *chr, const uint8_t *buf, int len)
  35{
  36    MuxChardev *d = MUX_CHARDEV(chr);
  37    int ret;
  38    if (!d->timestamps) {
  39        ret = qemu_chr_fe_write(&d->chr, buf, len);
  40    } else {
  41        int i;
  42
  43        ret = 0;
  44        for (i = 0; i < len; i++) {
  45            if (d->linestart) {
  46                char buf1[64];
  47                int64_t ti;
  48                int secs;
  49
  50                ti = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
  51                if (d->timestamps_start == -1) {
  52                    d->timestamps_start = ti;
  53                }
  54                ti -= d->timestamps_start;
  55                secs = ti / 1000;
  56                snprintf(buf1, sizeof(buf1),
  57                         "[%02d:%02d:%02d.%03d] ",
  58                         secs / 3600,
  59                         (secs / 60) % 60,
  60                         secs % 60,
  61                         (int)(ti % 1000));
  62                /* XXX this blocks entire thread. Rewrite to use
  63                 * qemu_chr_fe_write and background I/O callbacks */
  64                qemu_chr_fe_write_all(&d->chr,
  65                                      (uint8_t *)buf1, strlen(buf1));
  66                d->linestart = 0;
  67            }
  68            ret += qemu_chr_fe_write(&d->chr, buf + i, 1);
  69            if (buf[i] == '\n') {
  70                d->linestart = 1;
  71            }
  72        }
  73    }
  74    return ret;
  75}
  76
  77static const char * const mux_help[] = {
  78    "% h    print this help\n\r",
  79    "% x    exit emulator\n\r",
  80    "% s    save disk data back to file (if -snapshot)\n\r",
  81    "% t    toggle console timestamps\n\r",
  82    "% b    send break (magic sysrq)\n\r",
  83    "% c    switch between console and monitor\n\r",
  84    "% %  sends %\n\r",
  85    NULL
  86};
  87
  88int term_escape_char = 0x01; /* ctrl-a is used for escape */
  89static void mux_print_help(Chardev *chr)
  90{
  91    int i, j;
  92    char ebuf[15] = "Escape-Char";
  93    char cbuf[50] = "\n\r";
  94
  95    if (term_escape_char > 0 && term_escape_char < 26) {
  96        snprintf(cbuf, sizeof(cbuf), "\n\r");
  97        snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
  98    } else {
  99        snprintf(cbuf, sizeof(cbuf),
 100                 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
 101                 term_escape_char);
 102    }
 103    /* XXX this blocks entire thread. Rewrite to use
 104     * qemu_chr_fe_write and background I/O callbacks */
 105    qemu_chr_write_all(chr, (uint8_t *)cbuf, strlen(cbuf));
 106    for (i = 0; mux_help[i] != NULL; i++) {
 107        for (j = 0; mux_help[i][j] != '\0'; j++) {
 108            if (mux_help[i][j] == '%') {
 109                qemu_chr_write_all(chr, (uint8_t *)ebuf, strlen(ebuf));
 110            } else {
 111                qemu_chr_write_all(chr, (uint8_t *)&mux_help[i][j], 1);
 112            }
 113        }
 114    }
 115}
 116
 117static void mux_chr_send_event(MuxChardev *d, int mux_nr, int event)
 118{
 119    CharBackend *be = d->backends[mux_nr];
 120
 121    if (be && be->chr_event) {
 122        be->chr_event(be->opaque, event);
 123    }
 124}
 125
 126static int mux_proc_byte(Chardev *chr, MuxChardev *d, int ch)
 127{
 128    if (d->term_got_escape) {
 129        d->term_got_escape = 0;
 130        if (ch == term_escape_char) {
 131            goto send_char;
 132        }
 133        switch (ch) {
 134        case '?':
 135        case 'h':
 136            mux_print_help(chr);
 137            break;
 138        case 'x':
 139            {
 140                 const char *term =  "QEMU: Terminated\n\r";
 141                 qemu_chr_write_all(chr, (uint8_t *)term, strlen(term));
 142                 exit(0);
 143                 break;
 144            }
 145        case 's':
 146            blk_commit_all();
 147            break;
 148        case 'b':
 149            qemu_chr_be_event(chr, CHR_EVENT_BREAK);
 150            break;
 151        case 'c':
 152            assert(d->mux_cnt > 0); /* handler registered with first fe */
 153            /* Switch to the next registered device */
 154            mux_set_focus(chr, (d->focus + 1) % d->mux_cnt);
 155            break;
 156        case 't':
 157            d->timestamps = !d->timestamps;
 158            d->timestamps_start = -1;
 159            d->linestart = 0;
 160            break;
 161        }
 162    } else if (ch == term_escape_char) {
 163        d->term_got_escape = 1;
 164    } else {
 165    send_char:
 166        return 1;
 167    }
 168    return 0;
 169}
 170
 171static void mux_chr_accept_input(Chardev *chr)
 172{
 173    MuxChardev *d = MUX_CHARDEV(chr);
 174    int m = d->focus;
 175    CharBackend *be = d->backends[m];
 176
 177    while (be && d->prod[m] != d->cons[m] &&
 178           be->chr_can_read && be->chr_can_read(be->opaque)) {
 179        be->chr_read(be->opaque,
 180                     &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
 181    }
 182}
 183
 184static int mux_chr_can_read(void *opaque)
 185{
 186    MuxChardev *d = MUX_CHARDEV(opaque);
 187    int m = d->focus;
 188    CharBackend *be = d->backends[m];
 189
 190    if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE) {
 191        return 1;
 192    }
 193
 194    if (be && be->chr_can_read) {
 195        return be->chr_can_read(be->opaque);
 196    }
 197
 198    return 0;
 199}
 200
 201static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
 202{
 203    Chardev *chr = CHARDEV(opaque);
 204    MuxChardev *d = MUX_CHARDEV(opaque);
 205    int m = d->focus;
 206    CharBackend *be = d->backends[m];
 207    int i;
 208
 209    mux_chr_accept_input(opaque);
 210
 211    for (i = 0; i < size; i++)
 212        if (mux_proc_byte(chr, d, buf[i])) {
 213            if (d->prod[m] == d->cons[m] &&
 214                be && be->chr_can_read &&
 215                be->chr_can_read(be->opaque)) {
 216                be->chr_read(be->opaque, &buf[i], 1);
 217            } else {
 218                d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
 219            }
 220        }
 221}
 222
 223bool muxes_realized;
 224
 225void mux_chr_send_all_event(Chardev *chr, int event)
 226{
 227    MuxChardev *d = MUX_CHARDEV(chr);
 228    int i;
 229
 230    if (!muxes_realized) {
 231        return;
 232    }
 233
 234    /* Send the event to all registered listeners */
 235    for (i = 0; i < d->mux_cnt; i++) {
 236        mux_chr_send_event(d, i, event);
 237    }
 238}
 239
 240static void mux_chr_event(void *opaque, int event)
 241{
 242    mux_chr_send_all_event(CHARDEV(opaque), event);
 243}
 244
 245static GSource *mux_chr_add_watch(Chardev *s, GIOCondition cond)
 246{
 247    MuxChardev *d = MUX_CHARDEV(s);
 248    Chardev *chr = qemu_chr_fe_get_driver(&d->chr);
 249    ChardevClass *cc = CHARDEV_GET_CLASS(chr);
 250
 251    if (!cc->chr_add_watch) {
 252        return NULL;
 253    }
 254
 255    return cc->chr_add_watch(chr, cond);
 256}
 257
 258static void char_mux_finalize(Object *obj)
 259{
 260    MuxChardev *d = MUX_CHARDEV(obj);
 261    int i;
 262
 263    for (i = 0; i < d->mux_cnt; i++) {
 264        CharBackend *be = d->backends[i];
 265        if (be) {
 266            be->chr = NULL;
 267        }
 268    }
 269    qemu_chr_fe_deinit(&d->chr, false);
 270}
 271
 272void mux_chr_set_handlers(Chardev *chr, GMainContext *context)
 273{
 274    MuxChardev *d = MUX_CHARDEV(chr);
 275
 276    /* Fix up the real driver with mux routines */
 277    qemu_chr_fe_set_handlers(&d->chr,
 278                             mux_chr_can_read,
 279                             mux_chr_read,
 280                             mux_chr_event,
 281                             NULL,
 282                             chr,
 283                             context, true);
 284}
 285
 286void mux_set_focus(Chardev *chr, int focus)
 287{
 288    MuxChardev *d = MUX_CHARDEV(chr);
 289
 290    assert(focus >= 0);
 291    assert(focus < d->mux_cnt);
 292
 293    if (d->focus != -1) {
 294        mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
 295    }
 296
 297    d->focus = focus;
 298    mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
 299}
 300
 301static void qemu_chr_open_mux(Chardev *chr,
 302                              ChardevBackend *backend,
 303                              bool *be_opened,
 304                              Error **errp)
 305{
 306    ChardevMux *mux = backend->u.mux.data;
 307    Chardev *drv;
 308    MuxChardev *d = MUX_CHARDEV(chr);
 309
 310    drv = qemu_chr_find(mux->chardev);
 311    if (drv == NULL) {
 312        error_setg(errp, "mux: base chardev %s not found", mux->chardev);
 313        return;
 314    }
 315
 316    d->focus = -1;
 317    /* only default to opened state if we've realized the initial
 318     * set of muxes
 319     */
 320    *be_opened = muxes_realized;
 321    qemu_chr_fe_init(&d->chr, drv, errp);
 322}
 323
 324static void qemu_chr_parse_mux(QemuOpts *opts, ChardevBackend *backend,
 325                               Error **errp)
 326{
 327    const char *chardev = qemu_opt_get(opts, "chardev");
 328    ChardevMux *mux;
 329
 330    if (chardev == NULL) {
 331        error_setg(errp, "chardev: mux: no chardev given");
 332        return;
 333    }
 334    backend->type = CHARDEV_BACKEND_KIND_MUX;
 335    mux = backend->u.mux.data = g_new0(ChardevMux, 1);
 336    qemu_chr_parse_common(opts, qapi_ChardevMux_base(mux));
 337    mux->chardev = g_strdup(chardev);
 338}
 339
 340static void char_mux_class_init(ObjectClass *oc, void *data)
 341{
 342    ChardevClass *cc = CHARDEV_CLASS(oc);
 343
 344    cc->parse = qemu_chr_parse_mux;
 345    cc->open = qemu_chr_open_mux;
 346    cc->chr_write = mux_chr_write;
 347    cc->chr_accept_input = mux_chr_accept_input;
 348    cc->chr_add_watch = mux_chr_add_watch;
 349}
 350
 351static const TypeInfo char_mux_type_info = {
 352    .name = TYPE_CHARDEV_MUX,
 353    .parent = TYPE_CHARDEV,
 354    .class_init = char_mux_class_init,
 355    .instance_size = sizeof(MuxChardev),
 356    .instance_finalize = char_mux_finalize,
 357};
 358
 359static void register_types(void)
 360{
 361    type_register_static(&char_mux_type_info);
 362}
 363
 364type_init(register_types);
 365