DevSerial.cpp revision 44e4f016bd1daeefa4ee4fe7db5209f8abe25e85
/* $Id$ */
/** @file
* DevSerial - 16550A UART emulation.
*/
/*
* Copyright (C) 2006-2010 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/*
* This code is based on:
*
* QEMU 16550A UART emulation
*
* Copyright (c) 2003-2004 Fabrice Bellard
* Copyright (c) 2008 Citrix Systems, Inc.
*
* 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
* 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.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_DEV_SERIAL
#include <iprt/semaphore.h>
#include <iprt/critsect.h>
#include "../Builtins.h"
#undef VBOX_SERIAL_PCI /* The PCI variant has lots of problems: wrong IRQ line and wrong IO base assigned. */
#ifdef VBOX_SERIAL_PCI
#endif /* VBOX_SERIAL_PCI */
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
#define SERIAL_SAVED_STATE_VERSION_16450 3
#define SERIAL_SAVED_STATE_VERSION 4
/*
* These are the definitions for the Modem Control Register
*/
/*
* These are the definitions for the Modem Status Register
*/
/*
* Interrupt trigger levels.
* The byte-counts are for 16550A - in newer UARTs the byte-count for each ITL is higher.
*/
#define XMIT_FIFO 0
#define RECV_FIFO 1
#define MAX_XMIT_RETRY 8
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
struct SerialFifo
{
};
/**
* Serial device.
*
* @implements PDMIBASE
* @implements PDMICHARPORT
*/
struct SerialState
{
/** Access critical section. */
/** Pointer to the device instance - R3 Ptr. */
/** Pointer to the device instance - R0 Ptr. */
/** Pointer to the device instance - RC Ptr. */
/** Alignment. */
/** LUN\#0: The base interface. */
/** LUN\#0: The character port interface. */
/** Pointer to the attached base driver. */
/** Pointer to the attached character driver. */
/* NOTE: this hidden state is necessary for tx irq generation as
it can be reset while reading iir */
int thr_ipending;
int timeout_ipending;
int irq;
int last_break_enable;
/** Counter for retrying xmit */
int tsr_retry;
bool msr_changed;
bool fGCEnabled;
bool fR0Enabled;
bool fYieldOnLSRRead;
bool volatile fRecvWaiting;
bool f16550AEnabled;
bool Alignment3[2];
/** Time it takes to transmit a character */
#ifdef VBOX_SERIAL_PCI
#endif /* VBOX_SERIAL_PCI */
};
#ifndef VBOX_DEVICE_STRUCT_TESTCASE
#ifdef VBOX_SERIAL_PCI
#define PCIDEV_2_SERIALSTATE(pPciDev) ( (SerialState *)((uintptr_t)(pPciDev) - RT_OFFSETOF(SerialState, dev)) )
#endif /* VBOX_SERIAL_PCI */
#define PDMIBASE_2_SERIALSTATE(pInstance) ( (SerialState *)((uintptr_t)(pInterface) - RT_OFFSETOF(SerialState, IBase)) )
#define PDMICHARPORT_2_SERIALSTATE(pInstance) ( (SerialState *)((uintptr_t)(pInterface) - RT_OFFSETOF(SerialState, ICharPort)) )
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
PDMBOTHCBDECL(int) serialIOPortRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb);
PDMBOTHCBDECL(int) serialIOPortWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb);
#ifdef IN_RING3
static int serial_can_receive(SerialState *s);
{
f->count = 0;
f->head = 0;
f->tail = 0;
}
{
/* Receive overruns do not overwrite FIFO contents. */
{
if (f->head == UART_FIFO_LENGTH)
f->head = 0;
}
if (f->count < UART_FIFO_LENGTH)
f->count++;
s->lsr |= UART_LSR_OE;
return 1;
}
{
uint8_t c;
if (f->count == 0)
return 0;
if (f->tail == UART_FIFO_LENGTH)
f->tail = 0;
f->count--;
return c;
}
static void serial_update_irq(SerialState *s)
{
if ( (s->ier & UART_IER_RLSI)
&& (s->lsr & UART_LSR_INT_ANY)) {
/* Note that(s->ier & UART_IER_RDI) can mask this interrupt,
* this is not in the specification but is observed on existing
* hardware. */
} else if ( (s->ier & UART_IER_RDI)
&& (s->lsr & UART_LSR_DR)
&& ( !(s->fcr & UART_FCR_FE)
} else if ( (s->ier & UART_IER_THRI)
&& s->thr_ipending) {
} else if ( (s->ier & UART_IER_MSI)
&& (s->msr & UART_MSR_ANY_DELTA)) {
}
/** XXX only call the SetIrq function if the state really changes! */
if (tmp_iir != UART_IIR_NO_INT) {
# ifdef VBOX_SERIAL_PCI
# else /* !VBOX_SERIAL_PCI */
# endif /* !VBOX_SERIAL_PCI */
} else {
# ifdef VBOX_SERIAL_PCI
# else /* !VBOX_SERIAL_PCI */
# endif /* !VBOX_SERIAL_PCI */
}
}
static void serial_update_parameters(SerialState *s)
{
if (s->divider == 0)
return;
frame_size = 1;
if (s->lcr & 0x08) {
frame_size++;
if (s->lcr & 0x10)
parity = 'E';
else
parity = 'O';
} else {
parity = 'N';
}
if (s->lcr & 0x04)
stop_bits = 2;
else
stop_bits = 1;
}
static void serial_xmit(void *opaque)
{
if (s->tsr_retry <= 0) {
if (s->fcr & UART_FCR_FE) {
s->lsr |= UART_LSR_THRE;
} else {
s->lsr |= UART_LSR_THRE;
}
}
if (s->mcr & UART_MCR_LOOP) {
/* in loopback mode, say that we just received a char */
s->tsr_retry++;
return;
} else {
/* drop this character. */
s->tsr_retry = 0;
}
}
else {
s->tsr_retry = 0;
}
if (!(s->lsr & UART_LSR_THRE))
if (s->lsr & UART_LSR_THRE) {
s->lsr |= UART_LSR_TEMT;
s->thr_ipending = 1;
}
}
#endif /* IN_RING3 */
{
addr &= 7;
#ifndef IN_RING3
NOREF(s);
return VINF_IOM_HC_IOPORT_WRITE;
#else
switch(addr) {
default:
case 0:
if (s->lcr & UART_LCR_DLAB) {
} else {
if (s->fcr & UART_FCR_FE) {
s->thr_ipending = 0;
s->lsr &= ~UART_LSR_TEMT;
s->lsr &= ~UART_LSR_THRE;
} else {
s->thr_ipending = 0;
s->lsr &= ~UART_LSR_THRE;
}
serial_xmit(s);
}
break;
case 1:
if (s->lcr & UART_LCR_DLAB) {
} else {
if (s->lsr & UART_LSR_THRE) {
s->thr_ipending = 1;
}
}
break;
case 2:
if (!s->f16550AEnabled)
break;
break;
/* FIFO clear */
if (val & UART_FCR_RFR) {
s->timeout_ipending = 0;
fifo_clear(s, RECV_FIFO);
}
if (val & UART_FCR_XFR) {
fifo_clear(s, XMIT_FIFO);
}
if (val & UART_FCR_FE) {
s->iir |= UART_IIR_FE;
/* Set RECV_FIFO trigger Level */
switch (val & 0xC0) {
case UART_FCR_ITL_1:
break;
case UART_FCR_ITL_2:
break;
case UART_FCR_ITL_3:
break;
case UART_FCR_ITL_4:
break;
}
} else
s->iir &= ~UART_IIR_FE;
/* Set fcr - or at least the bits in it that are supposed to "stick" */
break;
case 3:
{
int break_enable;
if (break_enable != s->last_break_enable) {
{
}
}
}
break;
case 4:
{
!!(s->mcr & UART_MCR_RTS),
!!(s->mcr & UART_MCR_DTR));
}
break;
case 5:
break;
case 6:
break;
case 7:
break;
}
return VINF_SUCCESS;
#endif
}
{
*pRC = VINF_SUCCESS;
addr &= 7;
switch(addr) {
default:
case 0:
if (s->lcr & UART_LCR_DLAB) {
/* DLAB == 1: divisor latch (LS) */
} else {
#ifndef IN_RING3
#else
if (s->fcr & UART_FCR_FE) {
else
s->timeout_ipending = 0;
} else {
}
if (s->fRecvWaiting)
{
s->fRecvWaiting = false;
}
#endif
}
break;
case 1:
if (s->lcr & UART_LCR_DLAB) {
/* DLAB == 1: divisor latch (MS) */
} else {
}
break;
case 2:
#ifndef IN_RING3
#else
s->thr_ipending = 0;
}
/* reset msr changed bit */
s->msr_changed = false;
#endif
break;
case 3:
break;
case 4:
break;
case 5:
{
/* No data available and yielding is enabled, so yield in ring3. */
#ifndef IN_RING3
break;
#else
RTThreadYield ();
#endif
}
/* Clear break and overrun interrupts */
#ifndef IN_RING3
#else
#endif
}
break;
case 6:
if (s->mcr & UART_MCR_LOOP) {
/* in loopback, the modem output pins are connected to the
inputs */
} else {
/* Clear delta bits & msr int after read, if they were set */
if (s->msr & UART_MSR_ANY_DELTA) {
#ifndef IN_RING3
#else
s->msr &= 0xF0;
#endif
}
}
break;
case 7:
break;
}
return ret;
}
#ifdef IN_RING3
static int serial_can_receive(SerialState *s)
{
if (s->fcr & UART_FCR_FE) {
else
return 0;
} else {
return !(s->lsr & UART_LSR_DR);
}
}
{
if (s->fcr & UART_FCR_FE) {
int i;
for (i = 0; i < size; i++) {
}
s->lsr |= UART_LSR_DR;
/* call the timeout receive callback in 4 char transmit time */
} else {
if (s->lsr & UART_LSR_DR)
s->lsr |= UART_LSR_OE;
s->lsr |= UART_LSR_DR;
}
}
/** @copydoc PDMICHARPORT::pfnNotifyRead */
static DECLCALLBACK(int) serialNotifyRead(PPDMICHARPORT pInterface, const void *pvBuf, size_t *pcbRead)
{
{
if (!serial_can_receive(pThis))
{
/* If we cannot receive then wait for not more than 250ms. If we still
* cannot receive then the new character will either overwrite rbr
* or it will be dropped at fifo_put(). */
pThis->fRecvWaiting = true;
}
}
return VINF_SUCCESS;
}
/** @copydoc PDMICHARPORT::pfnNotifyStatusLinesChanged */
static DECLCALLBACK(int) serialNotifyStatusLinesChanged(PPDMICHARPORT pInterface, uint32_t newStatusLines)
{
/* Set new states. */
newMsr |= UART_MSR_DCD;
newMsr |= UART_MSR_RI;
newMsr |= UART_MSR_DSR;
newMsr |= UART_MSR_CTS;
/* Compare the old and the new states and set the delta bits accordingly. */
newMsr |= UART_MSR_DDCD;
newMsr |= UART_MSR_TERI;
newMsr |= UART_MSR_DDSR;
newMsr |= UART_MSR_DCTS;
pThis->msr_changed = true;
return VINF_SUCCESS;
}
/** @copydoc PDMICHARPORT::pfnNotifyBufferFull */
{
return VINF_SUCCESS;
}
/** @copydoc PDMICHARPORT::pfnNotifyBreak */
{
return VINF_SUCCESS;
}
/**
* Fifo timer functions.
*/
{
{
s->timeout_ipending = 1;
}
}
/**
* Transmit timer function.
* Just retry to transmit a character.
*
* @param pTimer The timer handle.
* @param pDevIns The device instance.
* @param pvUser The user pointer.
*/
{
serial_xmit(s);
}
/**
* Reset the serial device.
*
* @param pDevIns The device instance.
*/
{
s->rbr = 0;
s->ier = 0;
s->iir = UART_IIR_NO_INT;
s->lcr = 0;
/* Default to 9600 baud, 1 start bit, 8 data bits, 1 stop bit, no parity. */
s->divider = 0x0C;
s->mcr = UART_MCR_OUT2;
s->scr = 0;
s->tsr_retry = 0;
fifo_clear(s, RECV_FIFO);
fifo_clear(s, XMIT_FIFO);
s->thr_ipending = 0;
s->last_break_enable = 0;
# ifdef VBOX_SERIAL_PCI
# else /* !VBOX_SERIAL_PCI */
# endif /* !VBOX_SERIAL_PCI */
}
#endif /* IN_RING3 */
/**
* Port I/O Handler for OUT operations.
*
* @returns VBox status code.
*
* @param pDevIns The device instance.
* @param pvUser User argument.
* @param Port Port number used for the IN operation.
* @param u32 The value to output.
* @param cb The value size in bytes.
*/
{
int rc = VINF_SUCCESS;
if (cb == 1)
{
if (rc == VINF_SUCCESS)
{
}
}
else
return rc;
}
/**
* Port I/O Handler for IN operations.
*
* @returns VBox status code.
*
* @param pDevIns The device instance.
* @param pvUser User argument.
* @param Port Port number used for the IN operation.
* @param u32 The value to output.
* @param cb The value size in bytes.
*/
{
int rc = VINF_SUCCESS;
if (cb == 1)
{
if (rc == VINF_SUCCESS)
{
}
}
else
return rc;
}
#ifdef IN_RING3
/**
* @copydoc FNSSMDEVLIVEEXEC
*/
{
return VINF_SSM_DONT_CALL_AGAIN;
}
/**
* @copydoc FNSSMDEVSAVEEXEC
*/
{
/* Don't store:
* - the content of the FIFO
* - tsr_retry
*/
}
/**
* @copydoc FNSSMDEVLOADEXEC
*/
{
{
pThis->f16550AEnabled = false;
}
else
AssertMsgReturn(uVersion == SERIAL_SAVED_STATE_VERSION, ("%d\n", uVersion), VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION);
if (uPass == SSM_PASS_FINAL)
{
{
}
}
if (uPass == SSM_PASS_FINAL)
N_("Config mismatch - saved irq=%#x iobase=%#x; configured irq=%#x iobase=%#x"),
if (uPass == SSM_PASS_FINAL)
{
if (RT_FAILURE(rc))
return rc;
|| pThis->fRecvWaiting)
{
pThis->fRecvWaiting = false;
}
/* this isn't strictly necessary but cannot hurt... */
}
return VINF_SUCCESS;
}
/**
* @copydoc FNPDMDEVRELOCATE
*/
{
}
#ifdef VBOX_SERIAL_PCI
static DECLCALLBACK(int) serialIOPortRegionMap(PPCIDEVICE pPciDev, /* unsigned */ int iRegion, RTGCPHYS GCPhysAddress, uint32_t cb, PCIADDRESSSPACE enmType)
{
int rc = VINF_SUCCESS;
AssertMsg(RT_ALIGN(GCPhysAddress, 8) == GCPhysAddress, ("Expected 8 byte alignment. GCPhysAddress=%#x\n", GCPhysAddress));
/*
* Register our port IO handlers.
*/
return rc;
}
#endif /* VBOX_SERIAL_PCI */
/**
* @interface_method_impl{PDMIBASE, pfnQueryInterface}
*/
{
return NULL;
}
/**
* Destruct a device instance.
*
* Most VM resources are freed by the VM. This callback is provided so that any non-VM
* resources can be freed correctly.
*
* @returns VBox status.
* @param pDevIns The device instance data.
*/
{
return VINF_SUCCESS;
}
/**
* @interface_method_impl{PDMDEVREG, pfnConstruct}
*/
{
int rc;
/*
* Initialize the instance data.
* (Do this early or the destructor might choke on something!)
*/
/* IBase */
/* ICharPort */
#ifdef VBOX_SERIAL_PCI
/* the PCI device */
#endif /* VBOX_SERIAL_PCI */
/*
* Validate and read the configuration.
*/
"IOBase\0"
"GCEnabled\0"
"R0Enabled\0"
"YieldOnLSRRead\0"
"Enable16550A\0"
))
{
AssertMsgFailed(("serialConstruct Invalid configuration values\n"));
}
if (RT_FAILURE(rc))
N_("Configuration error: Failed to get the \"GCEnabled\" value"));
if (RT_FAILURE(rc))
N_("Configuration error: Failed to get the \"R0Enabled\" value"));
if (RT_FAILURE(rc))
N_("Configuration error: Failed to get the \"YieldOnLSRRead\" value"));
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
{
/* Provide sensible defaults. */
if (iInstance == 0)
irq_lvl = 4;
else if (iInstance == 1)
irq_lvl = 3;
else
AssertReleaseFailed(); /* irq_lvl is undefined. */
}
else if (RT_FAILURE(rc))
N_("Configuration error: Failed to get the \"IRQ\" value"));
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
{
if (iInstance == 0)
io_base = 0x3f8;
else if (iInstance == 1)
io_base = 0x2f8;
else
AssertReleaseFailed(); /* io_base is undefined */
}
else if (RT_FAILURE(rc))
N_("Configuration error: Failed to get the \"IOBase\" value"));
if (RT_FAILURE(rc))
N_("Configuration error: Failed to get the \"Enable16550A\" value"));
#ifdef VBOX_SERIAL_PCI
#else
#endif
LogRel(("Serial#%d: emulating %s\n", pDevIns->iInstance, pThis->f16550AEnabled ? "16550A" : "16450"));
/*
* Initialize critical section and the semaphore.
* This must of course be done before attaching drivers or anything else which can call us back..
*/
if (RT_FAILURE(rc))
return rc;
TMTIMER_FLAGS_NO_CRIT_SECT, "Serial Fifo Timer",
TMTIMER_FLAGS_NO_CRIT_SECT, "Serial Transmit Timer",
#ifdef VBOX_SERIAL_PCI
/*
* Register the PCI Device and region.
*/
if (RT_FAILURE(rc))
return rc;
if (RT_FAILURE(rc))
return rc;
#else /* !VBOX_SERIAL_PCI */
/*
* Register the I/O ports.
*/
if (RT_FAILURE(rc))
return rc;
if (pThis->fGCEnabled)
{
if (RT_FAILURE(rc))
return rc;
}
if (pThis->fR0Enabled)
{
if (RT_FAILURE(rc))
return rc;
}
#endif /* !VBOX_SERIAL_PCI */
/*
* Saved state.
*/
if (RT_FAILURE(rc))
return rc;
/*
* Attach the char driver and get the interfaces.
* For now no run-time changes are supported.
*/
if (RT_SUCCESS(rc))
{
{
return VERR_PDM_MISSING_INTERFACE;
}
/** @todo provide read notification interface!!!! */
}
else if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
{
}
else
{
/* Don't call VMSetError here as we assume that the driver already set an appropriate error */
return rc;
}
return VINF_SUCCESS;
}
/**
* The device registration structure.
*/
const PDMDEVREG g_DeviceSerialPort =
{
/* u32Version */
/* szName */
"serial",
/* szRCMod */
"VBoxDDGC.gc",
/* szR0Mod */
"VBoxDDR0.r0",
/* pszDescription */
"Serial Communication Port",
/* fFlags */
/* fClass */
/* cMaxInstances */
/* cbInstance */
sizeof(SerialState),
/* pfnConstruct */
/* pfnDestruct */
/* pfnRelocate */
/* pfnIOCtl */
NULL,
/* pfnPowerOn */
NULL,
/* pfnReset */
/* pfnSuspend */
NULL,
/* pfnResume */
NULL,
/* pfnAttach */
NULL,
/* pfnDetach */
NULL,
/* pfnQueryInterface. */
NULL,
/* pfnInitComplete */
NULL,
/* pfnPowerOff */
NULL,
/* pfnSoftReset */
NULL,
/* u32VersionEnd */
};
#endif /* IN_RING3 */
#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */