⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serial.c

📁 xen虚拟机源代码安装包
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * QEMU 16550A UART emulation *  * Copyright (c) 2003-2004 Fabrice Bellard *  * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */#include "vl.h"#include <sys/time.h>#include <time.h>#include <assert.h>#include <termios.h>#include <sys/ioctl.h>//#define DEBUG_SERIAL#define UART_LCR_DLAB	0x80	/* Divisor latch access bit */#define UART_IER_MSI	0x08	/* Enable Modem status interrupt */#define UART_IER_RLSI	0x04	/* Enable receiver line status interrupt */#define UART_IER_THRI	0x02	/* Enable Transmitter holding register int. */#define UART_IER_RDI	0x01	/* Enable receiver data interrupt */#define UART_IIR_NO_INT	0x01	/* No interrupts pending */#define UART_IIR_ID	0x06	/* Mask for the interrupt ID */#define UART_IIR_MSI	0x00	/* Modem status interrupt */#define UART_IIR_THRI	0x02	/* Transmitter holding register empty */#define UART_IIR_RDI	0x04	/* Receiver data interrupt */#define UART_IIR_RLSI	0x06	/* Receiver line status interrupt */#define UART_IIR_CTI    0x0C    /* Character Timeout Indication */#define UART_IIR_FENF   0x80    /* Fifo enabled, but not functionning */#define UART_IIR_FE     0xC0    /* Fifo enabled *//* * These are the definitions for the Modem Control Register */#define UART_MCR_LOOP	0x10	/* Enable loopback test mode */#define UART_MCR_OUT2	0x08	/* Out2 complement */#define UART_MCR_OUT1	0x04	/* Out1 complement */#define UART_MCR_RTS	0x02	/* RTS complement */#define UART_MCR_DTR	0x01	/* DTR complement *//* * These are the definitions for the Modem Status Register */#define UART_MSR_DCD	0x80	/* Data Carrier Detect */#define UART_MSR_RI	0x40	/* Ring Indicator */#define UART_MSR_DSR	0x20	/* Data Set Ready */#define UART_MSR_CTS	0x10	/* Clear to Send */#define UART_MSR_DDCD	0x08	/* Delta DCD */#define UART_MSR_TERI	0x04	/* Trailing edge ring indicator */#define UART_MSR_DDSR	0x02	/* Delta DSR */#define UART_MSR_DCTS	0x01	/* Delta CTS */#define UART_MSR_ANY_DELTA 0x0F	/* Any of the msr delta bits */#define UART_LSR_TEMT	0x40	/* Transmitter empty */#define UART_LSR_THRE	0x20	/* Transmit-hold-register empty */#define UART_LSR_BI	0x10	/* Break interrupt indicator */#define UART_LSR_FE	0x08	/* Frame error indicator */#define UART_LSR_PE	0x04	/* Parity error indicator */#define UART_LSR_OE	0x02	/* Overrun error indicator */#define UART_LSR_DR	0x01	/* Receiver data ready */#define UART_LSR_INT_ANY 0x1E	/* Any of the lsr-interrupt-triggering status bits *//* Interrupt trigger levels. The byte-counts are for 16550A - in newer UARTs the byte-count for each ITL is higher. */#define UART_FCR_ITL_1      0x00 /* 1 byte ITL */#define UART_FCR_ITL_2      0x40 /* 4 bytes ITL */#define UART_FCR_ITL_3      0x80 /* 8 bytes ITL */#define UART_FCR_ITL_4      0xC0 /* 14 bytes ITL */#define UART_FCR_DMS        0x08    /* DMA Mode Select */#define UART_FCR_XFR        0x04    /* XMIT Fifo Reset */#define UART_FCR_RFR        0x02    /* RCVR Fifo Reset */#define UART_FCR_FE         0x01    /* FIFO Enable */#define UART_FIFO_LENGTH    16      /* 16550A Fifo Length */#define XMIT_FIFO           0#define RECV_FIFO           1#define MAX_XMIT_RETRY      4 struct SerialFIFO {    uint8_t data[UART_FIFO_LENGTH];    uint8_t count;                          uint8_t itl;                        /* Interrupt Trigger Level */    uint8_t tail;    uint8_t head;} typedef SerialFIFO;struct SerialState {    uint16_t divider;    uint8_t rbr; /* receive register */    uint8_t thr; /* transmit holding register */    uint8_t tsr; /* transmit shift register */    uint8_t ier;    uint8_t iir; /* read only */    uint8_t lcr;    uint8_t mcr;    uint8_t lsr; /* read only */    uint8_t msr; /* read only */    uint8_t scr;    uint8_t fcr;    /* NOTE: this hidden state is necessary for tx irq generation as       it can be reset while reading iir */    int thr_ipending;    SetIRQFunc *set_irq;    void *irq_opaque;    int irq;    CharDriverState *chr;    int last_break_enable;    target_ulong base;    int it_shift;    int tsr_retry;    uint64_t last_xmit_ts;              /* Time when the last byte was successfully sent out of the tsr */    SerialFIFO recv_fifo;    SerialFIFO xmit_fifo;    struct QEMUTimer *fifo_timeout_timer;    int timeout_ipending;                   /* timeout interrupt pending state */    struct QEMUTimer *transmit_timer;    uint64_t char_transmit_time;               /* time to transmit a char in ticks*/    int poll_msl;    struct QEMUTimer *modem_status_poll;};/* Rate limit serial requests so that e.g. grub on a serial console   doesn't kill dom0.  Simple token bucket.  If we get some actual   data from the user, instantly refil the bucket. *//* How long it takes to generate a token, in microseconds. */#define TOKEN_PERIOD 1000/* Maximum and initial size of token bucket */#define TOKENS_MAX 100000static int tokens_avail;static void fifo_clear(SerialState *s, int fifo) {    SerialFIFO *f = ( fifo ) ? &s->recv_fifo : &s->xmit_fifo;    memset(f->data, 0, UART_FIFO_LENGTH);       f->count = 0;    f->head = 0;    f->tail = 0;}static int fifo_put(SerialState *s, int fifo, uint8_t chr) {    SerialFIFO *f = ( fifo ) ? &s->recv_fifo : &s->xmit_fifo;    f->data[f->head++] = chr;    if (f->head == UART_FIFO_LENGTH)        f->head = 0;    f->count++;    tokens_avail = TOKENS_MAX;    return 1;}uint8_t fifo_get(SerialState *s, int fifo) {    SerialFIFO *f = ( fifo ) ? &s->recv_fifo : &s->xmit_fifo;    uint8_t c;        if( f->count == 0 )        return 0;    c = f->data[f->tail++];    if (f->tail == UART_FIFO_LENGTH)        f->tail = 0;    f->count--;    tokens_avail = TOKENS_MAX;    return c; }static void serial_update_irq(SerialState *s){    uint8_t tmp_iir = UART_IIR_NO_INT;    if (!s->ier) {        s->set_irq(s->irq_opaque, s->irq, 0);	return;    }    if ( ( s->ier & UART_IER_RLSI ) && (s->lsr & UART_LSR_INT_ANY ) ) {        tmp_iir = UART_IIR_RLSI;    } else if ( s->timeout_ipending ) {        tmp_iir = UART_IIR_CTI;    } else if ( ( s->ier & UART_IER_RDI ) && (s->lsr & UART_LSR_DR ) ) {        if ( !(s->fcr & UART_FCR_FE) ) {           tmp_iir = UART_IIR_RDI;        } else if ( s->recv_fifo.count >= s->recv_fifo.itl ) {           tmp_iir = UART_IIR_RDI;        }    } else if ( (s->ier & UART_IER_THRI) && s->thr_ipending ) {        tmp_iir = UART_IIR_THRI;    } else if ( (s->ier & UART_IER_MSI) && (s->msr & UART_MSR_ANY_DELTA) ) {        tmp_iir = UART_IIR_MSI;    }    s->iir = tmp_iir | ( s->iir & 0xF0 );    if ( tmp_iir != UART_IIR_NO_INT ) {        s->set_irq(s->irq_opaque, s->irq, 1);    } else {        s->set_irq(s->irq_opaque, s->irq, 0);    }}static void serial_update_parameters(SerialState *s){    int speed, parity, data_bits, stop_bits, frame_size;    QEMUSerialSetParams ssp;    if (s->divider == 0)        return;    frame_size = 1;    if (s->lcr & 0x08) {        if (s->lcr & 0x10)            parity = 'E';        else            parity = 'O';    } else {            parity = 'N';            frame_size = 0;    }    if (s->lcr & 0x04)         stop_bits = 2;    else        stop_bits = 1;    data_bits = (s->lcr & 0x03) + 5;    frame_size += data_bits + stop_bits;    speed = 115200 / s->divider;    ssp.speed = speed;    ssp.parity = parity;    ssp.data_bits = data_bits;    ssp.stop_bits = stop_bits;    s->char_transmit_time =  ( ticks_per_sec / speed ) * frame_size;    qemu_chr_ioctl(s->chr, CHR_IOCTL_SERIAL_SET_PARAMS, &ssp);#if 0    printf("speed=%d parity=%c data=%d stop=%d\n",            speed, parity, data_bits, stop_bits);#endif}static void serial_get_token(void){    static struct timeval last_refil_time;    static int started;    assert(tokens_avail >= 0);    if (!tokens_avail) {	struct timeval delta, now;	int generated;	if (!started) {	    gettimeofday(&last_refil_time, NULL);	    tokens_avail = TOKENS_MAX;	    started = 1;	    return;	}    retry:	gettimeofday(&now, NULL);	delta.tv_sec = now.tv_sec - last_refil_time.tv_sec;	delta.tv_usec = now.tv_usec - last_refil_time.tv_usec;	if (delta.tv_usec < 0) {	    delta.tv_usec += 1000000;	    delta.tv_sec--;	}	assert(delta.tv_usec >= 0 && delta.tv_sec >= 0);	if (delta.tv_usec < TOKEN_PERIOD) {	    struct timespec ts;	    /* Wait until at least one token is available. */	    ts.tv_sec = TOKEN_PERIOD / 1000000;	    ts.tv_nsec = (TOKEN_PERIOD % 1000000) * 1000;	    while (nanosleep(&ts, &ts) < 0 && errno == EINTR)		;	    goto retry;	}	generated = (delta.tv_sec * 1000000) / TOKEN_PERIOD;	generated +=	    ((delta.tv_sec * 1000000) % TOKEN_PERIOD + delta.tv_usec) / TOKEN_PERIOD;	assert(generated > 0);	last_refil_time.tv_usec += (generated * TOKEN_PERIOD) % 1000000;	last_refil_time.tv_sec  += last_refil_time.tv_usec / 1000000;	last_refil_time.tv_usec %= 1000000;	last_refil_time.tv_sec  += (generated * TOKEN_PERIOD) / 1000000;	if (generated > TOKENS_MAX)	    generated = TOKENS_MAX;	tokens_avail = generated;    }    tokens_avail--;}static void serial_update_msl( SerialState *s ){    uint8_t omsr;    int flags;    qemu_del_timer(s->modem_status_poll);    if ( qemu_chr_ioctl(s->chr,CHR_IOCTL_SERIAL_GET_TIOCM, &flags) == -ENOTSUP ) {        s->poll_msl = -1;        return;    }        omsr = s->msr;    s->msr = ( flags & TIOCM_CTS ) ? s->msr | UART_MSR_CTS : s->msr & ~UART_MSR_CTS;    s->msr = ( flags & TIOCM_DSR ) ? s->msr | UART_MSR_DSR : s->msr & ~UART_MSR_DSR;    s->msr = ( flags & TIOCM_CAR ) ? s->msr | UART_MSR_DCD : s->msr & ~UART_MSR_DCD;    s->msr = ( flags & TIOCM_RI ) ? s->msr | UART_MSR_RI : s->msr & ~UART_MSR_RI;    if ( s->msr != omsr ) {         /* Set delta bits */         s->msr = s->msr | ( ( s->msr >> 4 ) ^ ( omsr >> 4 ) );         /* UART_MSR_TERI only if change was from 1 -> 0 */         if ( ( s->msr & UART_MSR_TERI ) && !( omsr & UART_MSR_RI ) )             s->msr &= ~UART_MSR_TERI;         serial_update_irq(s);    }    /* The real 16550A apparently has a 250ns response latency to line status changes.       We'll be lazy and poll only every 10ms, and only poll it at all if MSI interrupts are turned on */    if ( s->poll_msl )        qemu_mod_timer(s->modem_status_poll, qemu_get_clock(vm_clock) + ticks_per_sec / 100);}static void serial_xmit(void *opaque) {    SerialState *s = opaque;    uint64_t new_xmit_ts = qemu_get_clock(vm_clock);        if ( s->tsr_retry <= 0 ) {        if (s->fcr & UART_FCR_FE) {            s->tsr = fifo_get(s,XMIT_FIFO);            if ( !s->xmit_fifo.count )                s->lsr |= UART_LSR_THRE;        } else {            s->tsr = s->thr;            s->lsr |= UART_LSR_THRE;        }    }    if ( qemu_chr_write(s->chr, &s->tsr, 1) != 1 ) {        if ( ( s->tsr_retry > 0 ) && ( s->tsr_retry <= MAX_XMIT_RETRY ) ) {            s->tsr_retry++;            qemu_mod_timer(s->transmit_timer,  new_xmit_ts + s->char_transmit_time );            return;        } else if ( s->poll_msl < 0 ) {            /* If we exceed MAX_XMIT_RETRY and the backend is not a real serial port, then            drop any further failed writes instantly, until we get one that goes through.            This is to prevent guests that log to unconnected pipes or pty's from stalling. */            s->tsr_retry = -1;        }    }    else {        s->tsr_retry = 0;    }    s->last_xmit_ts = qemu_get_clock(vm_clock);    if ( !(s->lsr & UART_LSR_THRE) )        qemu_mod_timer(s->transmit_timer, s->last_xmit_ts + s->char_transmit_time );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -