serial fixes, including 2.2->2.1 migration
v3 cover serial fixes, including 2.2->2.1 migration
v3 manages to separate the migration fix from subsequent semantic changes, to make the backport as safe as possible.
Paolo Bonzini (4):
serial: reset thri_pending on IER writes with THRI=0
serial: clean up THRE/TEMT handling
serial: update LSR on enabling/disabling FIFOs
serial: only resample THR interrupt on rising edge of IER.THRI
hw/char/serial.c | 58 ++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 40 insertions(+), 18 deletions(-)
Signature · 1 line
1.8.3.1
PATCH v3 1/4 serial: reset thri_pending on IER writes with THRI=0
This is responsible for failure of migration from 2.2 to 2.1, because thr_ipending is always one in practice.
serial.c is setting thr_ipending unconditionally. However, thr_ipending is not used at all if THRI=0, and it will be overwritten again the next time THRE or THRI changes. For that reason, we can set thr_ipending to zero every time THRI is reset.
This has no semantic change and is enough to fix migration. It does not change the migration format, so 2.2.0 -> 2.1 will remain broken but we can fix 2.2.1 -> 2.1 without breaking 2.2.1 <-> 2.2.0.
There is disagreement on whether LSR.THRE should be resampled when IER.THRI goes from 1 to 1. Do not touch the code for now.
--- hw/char/serial.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-)
hw/char/serial.c +16 -2
@@ -350,10 +350,24 @@ static void serial_ioport_write(void *opaque, hwaddr addr, uint64_t val, s->poll_msl =0 ; } } -if (s->lsr &UART_LSR_THRE ) { + +/* Turning on the THRE interrupt on IER can trigger the interrupt +* if LSR.THRE=1, even if it had been masked before by reading IIR. +* This is not in the datasheet, but Windows relies on it. It is +* unclear if THRE has to be resampled every time THRI becomes +* 1, or only on the rising edge. Bochs does the latter, and Windows +* always toggles IER to all zeroes and back to all ones. But for +* now leave it as it has always been in QEMU. +* +* If IER.THRI is zero, thr_ipending is not used. Set it to zero +* so that the thr_ipending subsection is not migrated. +*/ +if ((s->ier &UART_IER_THRI ) && (s->lsr &UART_LSR_THRE )) { s->thr_ipending =1 ; -serial_update_irq (s); + }else { + s->thr_ipending =0 ; } +serial_update_irq (s); }break ;case 2 :
Signature · 1 line
1.8.3.1
Re: [Qemu-devel] [PATCH v3 1/4] serial: reset thri_pending on IER writes with THRI=0
Quoted 11 lines · * Paolo Bonzini ([email protected]) wrote:
This is responsible for failure of migration from 2.2 to 2.1, because
thr_ipendingis always one in practice.
serial.cis settingthr_ipendingunconditionally. However,thr_ipendingis not used at all if THRI=0, and it will be overwritten again the next time THRE or THRI changes. For that reason, we can setthr_ipendingto zero every time THRI is reset.This has no semantic change and is enough to fix migration. It does not change the migration format, so 2.2.0 -> 2.1 will remain broken but we can fix 2.2.1 -> 2.1 without breaking 2.2.1 <-> 2.2.0.
I can see this causes the thr_ipending to be 0 more of the time, but I don't see why it's sufficient.
If the transmitter has just transmitted it's last byte, then thr_ipending is set true by serial_xmit, however if there is a higher priority interrupt then serial_update_irq would set tmp_iir to something other than THRI, so I think serial_thr_ipending_needed would return true and write the subsection.
Dave
Quoted 45 lines
There is disagreement on whether LSR.THRE should be resampled when IER.THRI goes from 1 to 1. Do not touch the code for now.
Reported-by Igor Mammedov <[email protected]>Signed-off-by Paolo Bonzini <[email protected]>--- hw/char/serial.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-)hw/char/serial.c +16
-2@@ -350,10 +350,24 @@ static void serial_ioport_write(void *opaque, hwaddr addr, uint64_t val, s->poll_msl =0 ; } } -if (s->lsr &UART_LSR_THRE ) { + +/* Turning on the THRE interrupt on IER can trigger the interrupt +* if LSR.THRE=1, even if it had been masked before by reading IIR. +* This is not in the datasheet, but Windows relies on it. It is +* unclear if THRE has to be resampled every time THRI becomes +* 1, or only on the rising edge. Bochs does the latter, and Windows +* always toggles IER to all zeroes and back to all ones. But for +* now leave it as it has always been in QEMU. +* +* If IER.THRI is zero, thr_ipending is not used. Set it to zero +* so that the thr_ipending subsection is not migrated. +*/ +if ((s->ier &UART_IER_THRI ) && (s->lsr &UART_LSR_THRE )) { s->thr_ipending =1 ; -serial_update_irq (s); + }else { + s->thr_ipending =0 ; } +serial_update_irq (s); }break ;case 2 :Signature · 1 line
1.8.3.1
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [Qemu-devel] [PATCH v3 1/4] serial: reset thri_pending on IER writes with THRI=0
Quoted 8 lines · Dr. David Alan Gilbert
I can see this causes the
thr_ipendingto be 0 more of the time, but I don't see why it's sufficient.If the transmitter has just transmitted it's last byte, then
thr_ipendingis set true byserial_xmit, however if there is a higher priority interrupt thenserial_update_irqwould settmp_iirto something other than THRI, so I thinkserial_thr_ipending_neededwould return true and write the subsection.
It's not sufficient in all cases, and it cannot be. If we could always reconstruct the state, the subsection would not be necessary at all. Subsections can be used for two things:
- stuff that was not supported in the previous release, and thus would always be zero if you can start up the machine on the older release (with old machine type, no new devices or properties or CPU models). This is easy, but unfortunately not what this patch does.
- stuff that was migrated wrong in the previous release. Here, the game to play with the "needed" and "post_load" functions is to find a good-enough approximation of missing state from migrated state. This is what the thr_ipending subsection does: thr_ipending is approximated by "is the highest-priority active interrupt the THRI interrupt?"
So, in the case you mentioned 2.1->2.1 migration would have malfunctioned. The THRI would have been dropped after acknowledging the higher priority interrupt, and probably the guest driver would have hung (at least in the case of Windows). So, in this case 2.2->2.1 migration should fail, which the subsection achieves.
There are other examples of this in IDE, where PIO state was not migrated. Very early migration (during BIOS) would then hung the VM. The fix is to break backwards migration in this case, unfortunately you can't have it both ways. :(
Paolo
Dr. David Alan Gilbert
Reviewed-byRe: [Qemu-devel] [PATCH v3 1/4] serial: reset thri_pending on IER writes with THRI=0
Quoted 34 lines · * Paolo Bonzini ([email protected]) wrote:
Quoted 8 lines · Dr. David Alan Gilbert
I can see this causes the
thr_ipendingto be 0 more of the time, but I don't see why it's sufficient.If the transmitter has just transmitted it's last byte, then
thr_ipendingis set true byserial_xmit, however if there is a higher priority interrupt thenserial_update_irqwould settmp_iirto something other than THRI, so I thinkserial_thr_ipending_neededwould return true and write the subsection.It's not sufficient in all cases, and it cannot be. If we could always reconstruct the state, the subsection would not be necessary at all. Subsections can be used for two things:
- stuff that was not supported in the previous release, and thus would always be zero if you can start up the machine on the older release (with old machine type, no new devices or properties or CPU models). This is easy, but unfortunately not what this patch does.
- stuff that was migrated wrong in the previous release. Here, the game to play with the "needed" and "
post_load" functions is to find a good-enough approximation of missing state from migrated state. This is what thethr_ipendingsubsection does:thr_ipendingis approximated by "is the highest-priority active interrupt the THRI interrupt?"So, in the case you mentioned 2.1->2.1 migration would have malfunctioned. The THRI would have been dropped after acknowledging the higher priority interrupt, and probably the guest driver would have hung (at least in the case of Windows). So, in this case 2.2->2.1 migration should fail, which the subsection achieves.
Quoted 4 lines
There are other examples of this in IDE, where PIO state was not migrated. Very early migration (during BIOS) would then hung the VM. The fix is to break backwards migration in this case, unfortunately you can't have it both ways. :(
I think the situation for different versions is:
a pre-2.2 Guest could lose an interrupt, may hang serial transmit
b 2.2 2.2->2.1 fails migration most of the time
c 2.1->2.2 better chance than older since it tries to
reconstruct thr_ipending from iir state, but
can still lose interrupt
d 2.2->2.2 works
e your patch 2.2->2.1 still might fail migration if unlucky not common
f 2.1->2.2 as (c)
g 2.2<->patch works
h patch<->patch worksAnyone who really cares about backwards migration compatibility would probably have to guard the subsection with the machine-type to avoid
(e) ever happening (the heuristic from (c) might be useful to add).
So since (e) is an improvement:
although if you do have to recut it, please clarify the text that says
'and is enough to fix migration.' since it doesn't quite.
(This is an interesting example where with a migration format that allowed 'optional' subsections you wouldn't break backwards migration if the reader could ignore a section marked as such that it didn't recognise).
Quoted 2 lines
Paolo
Dave
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [Qemu-devel] [PATCH v3 1/4] serial: reset thri_pending on IER writes with THRI=0
Quoted 3 lines · Dr. David Alan Gilbert
Anyone who really cares about backwards migration compatibility would probably have to guard the subsection with the machine-type to avoid
(e) ever happening (the heuristic from (c) might be useful to add).
Right.
Quoted 2 lines
although if you do have to recut it, please clarify the text that says
'and is enough to fix migration.' since it doesn't quite.
Ok, I'll clarify this, specifying which case remains broken by design.
Quoted 3 lines
(This is an interesting example where with a migration format that allowed 'optional' subsections you wouldn't break backwards migration if the reader could ignore a section marked as such that it didn't recognise).
Right, the question is whether you really want to do this. :)
Paolo
Re: [Qemu-devel] [PATCH v3 1/4] serial: reset thri_pending on IER writes with THRI=0
Quoted 12 lines · * Paolo Bonzini ([email protected]) wrote:
Quoted 3 lines · Dr. David Alan Gilbert
Anyone who really cares about backwards migration compatibility would probably have to guard the subsection with the machine-type to avoid
(e) ever happening (the heuristic from (c) might be useful to add).Right.
Quoted 2 lines
although if you do have to recut it, please clarify the text that says
'and is enough to fix migration.' since it doesn't quite.
Quoted 7 lines
Ok, I'll clarify this, specifying which case remains broken by design.
Quoted 3 lines
(This is an interesting example where with a migration format that allowed 'optional' subsections you wouldn't break backwards migration if the reader could ignore a section marked as such that it didn't recognise).
Right, the question is whether you really want to do this. :)
Well, it would solve this type of problem less painfully, your migration would always succeed and destinations that knew what to do with it would always do the right thing. Of course you could also solve the problem if the source knew the version of the destination, but you didn't like that idea either.
Dave
Quoted 2 lines
Paolo
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [Qemu-devel] [PATCH v3 1/4] serial: reset thri_pending on IER writes with THRI=0
Quoted 4 lines · Dr. David Alan Gilbert
Quoted 1 line
Quoted 1 line
Right, the question is whether you really want to do this. :)
Well, it would solve this type of problem less painfully, your migration would always succeed and destinations that knew what to do with it would always do the right thing.
Yes, that's a determination to make. I agree that this particular case is one where losing the subsection is not the end of the world, and keying off the machine type could be a good call for downstreams.
But that's also because downstreams can also patch older source versions, while QEMU will not release another 2.1 version. In fact...
Quoted 3 lines
Of course you could also solve the problem
if the source knew the version of the destination, but you didn't like that idea either.
... if the source could be patched by someone to know the version of the destination, they could also add the subsection with a "needed" function that always returns 0. This way it is never migrated, but it is interpreted. You do not need the version of the destination.
Paolo
PATCH v3 2/4 serial: clean up THRE/TEMT handling
In reply to Paolo Bonzini- assert THRE cleared and FIFO not empty (if enabled) before sending a character. Also assert TEMT cleared, since it is the combination of THRE && transmitter shift register empty.
- raise THRI immediately after setting THRE
- check THRE to see if another character has to be sent, which makes the assertions more obvious and also means TEMT has to be set as soon as the loop ends
- clear TEMT together with THRE even in the non-FIFO case
There are certainly a couple bugfixes in here, but nothing that squashes known bugs.
--- hw/char/serial.c | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-)
hw/char/serial.c +12 -14
@@ -224,21 +224,23 @@ static gboolean serial_xmit(GIOChannel *chan, GIOCondition cond, void *opaque)SerialState *s = opaque; do { + assert(!(s->lsr & UART_LSR_TEMT)); + assert(!(s->lsr & UART_LSR_THRE)); + if (s->tsr_retry <= 0) { if (s->fcr & UART_FCR_FE) { - if (fifo8_is_empty(&s->xmit_fifo)) { - return FALSE; - } + assert(!fifo8_is_empty(&s->xmit_fifo)); s->tsr = fifo8_pop(&s->xmit_fifo); if (!s->xmit_fifo.num) { s->lsr |= UART_LSR_THRE; } - } else if ((s->lsr & UART_LSR_THRE)) { - return FALSE; } else { s->tsr = s->thr; s->lsr |= UART_LSR_THRE; - s->lsr &= ~UART_LSR_TEMT; + } + if ((s->lsr & UART_LSR_THRE) && !s->thr_ipending) { + s->thr_ipending = 1; + serial_update_irq(s); } }
@@ -256,17 +258,13 @@ static gboolean serial_xmit(GIOChannel *chan, GIOCondition cond, void *opaque) }else { s->tsr_retry =0 ; } +/* Transmit another byte if it is already available. It is only possible when FIFO is enabled and not empty. */ - }while ((s->fcr &UART_FCR_FE ) && !fifo8_is_empty (&s->xmit_fifo)); + }while (!(s->lsr &UART_LSR_THRE )); s->last_xmit_ts =qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ); - -if (s->lsr &UART_LSR_THRE ) { - s->lsr |=UART_LSR_TEMT ; - s->thr_ipending =1 ; -serial_update_irq (s); - } + s->lsr |=UART_LSR_TEMT ;return FALSE; }
@@ -323,10 +321,10 @@ static void serial_ioport_write(void *opaque, hwaddr addr, uint64_t val,fifo8_pop (&s->xmit_fifo); }fifo8_push (&s->xmit_fifo, s->thr); - s->lsr &= ~UART_LSR_TEMT ; } s->thr_ipending =0 ; s->lsr &= ~UART_LSR_THRE ; + s->lsr &= ~UART_LSR_TEMT ;serial_update_irq (s);if (s->tsr_retry <=0 ) {serial_xmit (NULL ,G_IO_OUT , s);
Signature · 1 line
1.8.3.1
Re: [Qemu-devel] [PATCH v3 2/4] serial: clean up THRE/TEMT handling
Quoted 27 lines · * Paolo Bonzini ([email protected]) wrote:
- assert THRE cleared and FIFO not empty (if enabled) before sending a character. Also assert TEMT cleared, since it is the combination of THRE && transmitter shift register empty.
- raise THRI immediately after setting THRE
- check THRE to see if another character has to be sent, which makes the assertions more obvious and also means TEMT has to be set as soon as the loop ends
- clear TEMT together with THRE even in the non-FIFO case
There are certainly a couple bugfixes in here, but nothing that squashes known bugs.
Signed-off-by Paolo Bonzini <[email protected]>--- hw/char/serial.c | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-)hw/char/serial.c +0
-0@@ -224,21 +224,23 @@ static gboolean serial_xmit(GIOChannel *chan, GIOCondition cond, void *opaque)SerialState *s = opaque;
hw/char/serial.c static gboolean serial_xmit(GIOChannel *chan, GIOCondition cond, void *opaque) · line 232
Quoted 10 lines
hw/char/serial.c +4
-3do { + assert(!(s->lsr & UART_LSR_TEMT)); + assert(!(s->lsr & UART_LSR_THRE)); + if (s->tsr_retry <= 0) { if (s->fcr & UART_FCR_FE) { - if (fifo8_is_empty(&s->xmit_fifo)) { - return FALSE; - } + assert(!fifo8_is_empty(&s->xmit_fifo));
That's undoing [email protected]'s
dffacd46 - Fix emptyness checking
See, http://permalink.gmane.org/gmane.comp.emulators.qemu/262412
I don't think your assumptions are safe because of that qemu_chr_fe_add_watch.
Dave
Quoted 54 lines
hw/char/serial.c +8
-11s->tsr =fifo8_pop (&s->xmit_fifo);if (!s->xmit_fifo.num) { s->lsr |=UART_LSR_THRE ; } - }else if ((s->lsr &UART_LSR_THRE )) { -return FALSE; }else { s->tsr = s->thr; s->lsr |=UART_LSR_THRE ; - s->lsr &= ~UART_LSR_TEMT ; + } +if ((s->lsr &UART_LSR_THRE ) && !s->thr_ipending) { + s->thr_ipending =1 ; +serial_update_irq (s); } }@@ -256,17 +258,13 @@ static gboolean serial_xmit(GIOChannel *chan, GIOCondition cond, void *opaque) }else { s->tsr_retry =0 ; } +/* Transmit another byte if it is already available. It is only possible when FIFO is enabled and not empty. */ - }while ((s->fcr &UART_FCR_FE ) && !fifo8_is_empty (&s->xmit_fifo)); + }while (!(s->lsr &UART_LSR_THRE )); s->last_xmit_ts =qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ); - -if (s->lsr &UART_LSR_THRE ) { - s->lsr |=UART_LSR_TEMT ; - s->thr_ipending =1 ; -serial_update_irq (s); - } + s->lsr |=UART_LSR_TEMT ;return FALSE; }@@ -323,10 +321,10 @@ static void serial_ioport_write(void *opaque, hwaddr addr, uint64_t val,fifo8_pop (&s->xmit_fifo); }fifo8_push (&s->xmit_fifo, s->thr); - s->lsr &= ~UART_LSR_TEMT ; } s->thr_ipending =0 ; s->lsr &= ~UART_LSR_THRE ; + s->lsr &= ~UART_LSR_TEMT ;serial_update_irq (s);if (s->tsr_retry <=0 ) {serial_xmit (NULL ,G_IO_OUT , s);Signature · 1 line
1.8.3.1
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [Qemu-devel] [PATCH v3 2/4] serial: clean up THRE/TEMT handling
Quoted 10 lines · Dr. David Alan Gilbert
Quoted 10 lines
hw/char/serial.c static gboolean serial_xmit(GIOChannel *chan, GIOCondition cond, void *opaque) · line 232
Quoted 10 lines
do {hw/char/serial.c +4
-3+assert (!(s->lsr &UART_LSR_TEMT )); +assert (!(s->lsr &UART_LSR_THRE )); +if (s->tsr_retry <=0 ) {if (s->fcr &UART_FCR_FE ) { -if (fifo8_is_empty (&s->xmit_fifo)) { -return FALSE; - } +assert (!fifo8_is_empty (&s->xmit_fifo));
Quoted 6 lines
That's undoing [email protected]'s
dffacd46- Fix emptyness checkingSee, http://permalink.gmane.org/gmane.comp.emulators.qemu/262412
I don't think your assumptions are safe because of thatqemu_chr_fe_add_watch.
I think it's safe because:
- serial_xmit is called from outside only after resetting TEMT and THRE and pushing a character on the FIFO
- serial_xmit iterates a second time over do...while() only if the FIFO is not empty (both before and after this patch; this patch only changes the condition that is used)
- if qemu_chr_fe_add_watch is called, the next call will have tsr_retry
Quoted 1 line
= 1 and thus the "if" would be skipped.
Note that in the middle we had commit f702e62 (serial: change retry logic to avoid concurrency, 2014-07-11) that fixed some messy behavior of qemu_chr_fe_add_watch. The commit message talks about multiple calls to qemu_chr_fe_add_watch triggering s->tsr_retry >= MAX_XMIT_RETRY but this is not the only possible failure. If you have multiple calls, the subsequent ones will see s->tsr_retry == 0 and will find (s->lsr & UART_LSR_THRE) != 0 on entry. But this should really never happen.
(Thanks for making me think more about it. :))
Paolo
Re: [Qemu-devel] [PATCH v3 2/4] serial: clean up THRE/TEMT handling
Quoted 17 lines · * Paolo Bonzini ([email protected]) wrote:
Quoted 14 lines · Dr. David Alan Gilbert
Quoted 10 lines
hw/char/serial.c static gboolean serial_xmit(GIOChannel *chan, GIOCondition cond, void *opaque) · line 232
Quoted 10 lines
do {hw/char/serial.c +4
-3+assert (!(s->lsr &UART_LSR_TEMT )); +assert (!(s->lsr &UART_LSR_THRE )); +if (s->tsr_retry <=0 ) {if (s->fcr &UART_FCR_FE ) { -if (fifo8_is_empty (&s->xmit_fifo)) { -return FALSE; - } +assert (!fifo8_is_empty (&s->xmit_fifo));That's undoing [email protected]'s
dffacd46- Fix emptyness checking
Quoted 7 lines
Quoted 2 lines
See, http://permalink.gmane.org/gmane.comp.emulators.qemu/262412
I don't think your assumptions are safe because of thatqemu_chr_fe_add_watch.I think it's safe because:
-
serial_xmitis called from outside only after resetting TEMT and THRE and pushing a character on the FIFO
Are you sure about TEMT? My reading of serial_ioport_write is that if !FCR_FE then TEMT isn't cleared.
Quoted 15 lines
-
serial_xmititerates a second time over do...while() only if the FIFO is not empty (both before and after this patch; this patch only changes the condition that is used)- if
qemu_chr_fe_add_watchis called, the next call will havetsr_retryQuoted 1 line
= 1 and thus the "if" would be skipped.
Note that in the middle we had commit f702e62 (serial: change retry logic to avoid concurrency, 2014-07-11) that fixed some messy behavior of
qemu_chr_fe_add_watch. The commit message talks about multiple calls toqemu_chr_fe_add_watchtriggerings->tsr_retry>=MAX_XMIT_RETRYbut this is not the only possible failure. If you have multiple calls, the subsequent ones will sees->tsr_retry== 0 and will find (s->lsr&UART_LSR_THRE) != 0 on entry. But this should really never happen.
Quoted 1 line
(Thanks for making me think more about it. :))
Ah yes, that changed things around a lot.
Dave
Quoted 2 lines
Paolo
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [Qemu-devel] [PATCH v3 2/4] serial: clean up THRE/TEMT handling
Quoted 5 lines · Dr. David Alan Gilbert
Quoted 3 lines
Quoted 3 lines
-
serial_xmitis called from outside only after resetting TEMT and THRE and pushing a character on the FIFOAre you sure about TEMT? My reading of
serial_ioport_writeis that if !FCR_FE then TEMT isn't cleared.
This patch changes that.
Paolo
Dr. David Alan Gilbert
Reviewed-byRe: [Qemu-devel] [PATCH v3 2/4] serial: clean up THRE/TEMT handling
Quoted 10 lines · * Paolo Bonzini ([email protected]) wrote:
Quoted 5 lines · Dr. David Alan Gilbert
Quoted 3 lines
Quoted 3 lines
-
serial_xmitis called from outside only after resetting TEMT and THRE and pushing a character on the FIFOAre you sure about TEMT? My reading of
serial_ioport_writeis that if !FCR_FE then TEMT isn't cleared.This patch changes that.
Oh yeh!
Quoted 2 lines
Paolo
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
PATCH v3 3/4 serial: update LSR on enabling/disabling FIFOs
In reply to Paolo BonziniWhen the transmit FIFO is emptied or enabled, the transmitter hold register is empty. When it is disabled, it is also emptied and in addition the previous contents of the transmitter hold register are discarded. In either case, the THRE bit in LSR must be set and THRI raised.
When the receive FIFO is emptied or enabled, the data ready and break bits must be cleared in LSR. Likewise when the receive FIFO is disabled.
--- hw/char/serial.c | 3 +++ 1 file changed, 3 insertions(+)
hw/char/serial.c +3 -0
@@ -377,12 +377,15 @@ static void serial_ioport_write(void *opaque, hwaddr addr, uint64_t val,/* FIFO clear */ if (val &UART_FCR_RFR ) { + s->lsr &= ~(UART_LSR_DR |UART_LSR_BI );timer_del (s->fifo_timeout_timer); s->timeout_ipending =0 ;fifo8_reset (&s->recv_fifo); }if (val &UART_FCR_XFR ) { + s->lsr |=UART_LSR_THRE ; + s->thr_ipending =1 ;fifo8_reset (&s->xmit_fifo); }
Signature · 1 line
1.8.3.1
Re: [Qemu-devel] [PATCH v3 3/4] serial: update LSR on enabling/disabling FIFOs
Quoted 23 lines · * Paolo Bonzini ([email protected]) wrote:
When the transmit FIFO is emptied or enabled, the transmitter hold register is empty. When it is disabled, it is also emptied and in addition the previous contents of the transmitter hold register are discarded. In either case, the THRE bit in LSR must be set and THRI raised.
When the receive FIFO is emptied or enabled, the data ready and break bits must be cleared in LSR. Likewise when the receive FIFO is disabled.
Signed-off-by Paolo Bonzini <[email protected]>--- hw/char/serial.c | 3 +++ 1 file changed, 3 insertions(+)hw/char/serial.c +1
-0@@ -377,12 +377,15 @@ static void serial_ioport_write(void *opaque, hwaddr addr, uint64_t val,/* FIFO clear */ if (val &UART_FCR_RFR ) { + s->lsr &= ~(UART_LSR_DR |UART_LSR_BI );
hw/char/serial.c static void serial_ioport_write(void *opaque, hwaddr addr, uint64_t val, · line 390
Quoted 10 lines
hw/char/serial.c +2
-0timer_del (s->fifo_timeout_timer); s->timeout_ipending =0 ;fifo8_reset (&s->recv_fifo); }if (val &UART_FCR_XFR ) { + s->lsr |=UART_LSR_THRE ; + s->thr_ipending =1 ;fifo8_reset (&s->xmit_fifo); }
Doesn't that break the assertion you added in patch 2? i.e. if I write a character, but it can't be sent, so it's added to the tsr_retry, but before the callback I set FCR_XFR, and that now sets LSR_THRE, then the callback triggers and it hits the assert?
Dave
Quoted 5 lines
Signature · 2 lines
-- 1.8.3.1
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [Qemu-devel] [PATCH v3 3/4] serial: update LSR on enabling/disabling FIFOs
Quoted 11 lines · Dr. David Alan Gilbert
Quoted 6 lines
hw/char/serial.c static void serial_ioport_write(void *opaque, hwaddr addr, uint64_t val, · line 390
Quoted 6 lines
if (val & UART_FCR_XFR) {hw/char/serial.c +2
-0+ s->lsr |=UART_LSR_THRE ; + s->thr_ipending =1 ;fifo8_reset (&s->xmit_fifo); }Doesn't that break the assertion you added in patch 2? i.e. if I write a character, but it can't be sent, so it's added to the
tsr_retry, but before the callback I setFCR_XFR, and that now setsLSR_THRE, then the callback triggers and it hits the assert?
You're right. The TEMT assertion is okay, but the THRE assertion should be inside if (s->tsr_retry <= 0).
Paolo
PATCH v3 4/4 serial: only resample THR interrupt on rising edge of IER.THRI
In reply to Paolo BonziniThere is disagreement on whether LSR.THRE should be resampled when IER.THRI goes from 1 to 1. Bochs only does it if IER.THRI goes from 0 to 1; PCE does it even if IER.THRI is unchanged. But the Windows driver seems to always go from 1 to 0 and back to 1, so do things in agreement with Bochs, because the handling of thr_ipending was reported in 2010
(https://lists.gnu.org/archive/html/qemu-devel/2010-03/msg01914.html)
as breaking DR-DOS Plus.
--- hw/char/serial.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-)
hw/char/serial.c +16 -9
@@ -336,10 +336,12 @@ static void serial_ioport_write(void *opaque, hwaddr addr, uint64_t val, s->divider = (s->divider &0x00ff ) | (val <<8 );serial_update_parameters (s); }else { +int changed = (s->ier ^ val) &0x0f ; s->ier = val &0x0f ;/* If the backend device is a real serial port, turn polling of the modem -status lines on physical port on or off depending on UART_IER_MSI state */ -if (s->poll_msl >=0 ) { +* status lines on physical port on or off depending on UART_IER_MSI state. +*/ +if ((changed &UART_IER_MSI ) && s->poll_msl >=0 ) {if (s->ier &UART_IER_MSI ) { s->poll_msl =1 ;serial_update_msl (s);
@@ -354,18 +356,23 @@ static void serial_ioport_write(void *opaque, hwaddr addr, uint64_t val,* This is not in the datasheet, but Windows relies on it. It is * unclear if THRE has to be resampled every time THRI becomes * 1, or only on the rising edge. Bochs does the latter, and Windows -* always toggles IER to all zeroes and back to all ones. But for -* now leave it as it has always been in QEMU. +* always toggles IER to all zeroes and back to all ones, so do the +* same. * * If IER.THRI is zero, thr_ipending is not used. Set it to zero * so that the thr_ipending subsection is not migrated. */ -if ((s->ier &UART_IER_THRI ) && (s->lsr &UART_LSR_THRE )) { - s->thr_ipending =1 ; - }else { - s->thr_ipending =0 ; +if (changed &UART_IER_THRI ) { +if ((s->ier &UART_IER_THRI ) && (s->lsr &UART_LSR_THRE )) { + s->thr_ipending =1 ; + }else { + s->thr_ipending =0 ; + } + } + +if (changed) { +serial_update_irq (s); } -serial_update_irq (s); }break ;case 2 :
Signature · 1 line
1.8.3.1
Re: [Qemu-devel] [PATCH v3 4/4] serial: only resample THR interrupt on rising edge of IER.THRI
Quoted 19 lines · * Paolo Bonzini ([email protected]) wrote:
There is disagreement on whether LSR.THRE should be resampled when IER.THRI goes from 1 to 1. Bochs only does it if IER.THRI goes from 0 to 1; PCE does it even if IER.THRI is unchanged. But the Windows driver seems to always go from 1 to 0 and back to 1, so do things in agreement with Bochs, because the handling of
thr_ipendingwas reported in 2010
(https://lists.gnu.org/archive/html/qemu-devel/2010-03/msg01914.html)
as breaking DR-DOS Plus.Reported-by Roy Tam <[email protected]>Signed-off-by Paolo Bonzini <[email protected]>--- hw/char/serial.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-)hw/char/serial.c +0
-0
hw/char/serial.c static void serial_ioport_write(void *opaque, hwaddr addr, uint64_t val, · line 339
Quoted 4 lines
hw/char/serial.c +1
-0@@ -336,10 +336,12 @@ static void serial_ioport_write(void *opaque, hwaddr addr, uint64_t val, s->divider = (s->divider &0x00ff ) | (val <<8 );serial_update_parameters (s); }else { +int changed = (s->ier ^ val) &0x0f ;
uint8_t perhaps?
hw/char/serial.c static void serial_ioport_write(void *opaque, hwaddr addr, uint64_t val, · line 347
Quoted 10 lines
hw/char/serial.c +3
-2s->ier = val &0x0f ;/* If the backend device is a real serial port, turn polling of the modem -status lines on physical port on or off depending on UART_IER_MSI state */ -if (s->poll_msl >=0 ) { +* status lines on physical port on or off depending on UART_IER_MSI state. +*/ +if ((changed &UART_IER_MSI ) && s->poll_msl >=0 ) {if (s->ier &UART_IER_MSI ) { s->poll_msl =1 ;serial_update_msl (s);
This MSI stuff, this change is just intended to do the same as you're
doing for THRI and making it change based? The commit message and title
doens't mention it.
Quoted 20 lines
hw/char/serial.c +6
-6@@ -354,18 +356,23 @@ static void serial_ioport_write(void *opaque, hwaddr addr, uint64_t val,* This is not in the datasheet, but Windows relies on it. It is * unclear if THRE has to be resampled every time THRI becomes * 1, or only on the rising edge. Bochs does the latter, and Windows -* always toggles IER to all zeroes and back to all ones. But for -* now leave it as it has always been in QEMU. +* always toggles IER to all zeroes and back to all ones, so do the +* same. * * If IER.THRI is zero, thr_ipending is not used. Set it to zero * so that the thr_ipending subsection is not migrated. */ -if ((s->ier &UART_IER_THRI ) && (s->lsr &UART_LSR_THRE )) { - s->thr_ipending =1 ; - }else { - s->thr_ipending =0 ; +if (changed &UART_IER_THRI ) { +if ((s->ier &UART_IER_THRI ) && (s->lsr &UART_LSR_THRE )) { + s->thr_ipending =1 ; + }else {
hw/char/serial.c static void serial_ioport_write(void *opaque, hwaddr addr, uint64_t val, · line 377
Quoted 10 lines
hw/char/serial.c +6
-1+ s->thr_ipending =0 ; + } + } + +if (changed) { +serial_update_irq (s); } -serial_update_irq (s); }break ;
But the rest of this looks OK.
Dave
Quoted 4 lines
hw/char/serial.c +0
-0case 2 :Signature · 1 line
1.8.3.1
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [Qemu-devel] [PATCH v3 4/4] serial: only resample THR interrupt on rising edge of IER.THRI
Quoted 8 lines · Dr. David Alan Gilbert
Quoted 4 lines
+ if ((changed & UART_IER_MSI) && s->poll_msl >= 0) { if (s->ier & UART_IER_MSI) { s->poll_msl = 1; serial_update_msl(s);This MSI stuff, this change is just intended to do the same as you're
doing for THRI and making it change based? The commit message and title
doens't mention it.
It has no change. If MSI = 1, there is already a timer ticking every 100th of a second to poll the modem status, so it's useless to go through serial_udpate_msl/timer_del/...
It's just because I have the changed bits at hand now.
BTW, looks like the poll_msl subsection after all was not necessary. It can be gleaned by ANDing "does CHR_IOCTL_SERIAL_GET_TIOCM work?" (which is immutable state depending on the command-line configuration" with IER.MSI.
Paolo