From owner-freebsd-arch@FreeBSD.ORG Sun Jun 15 12:02:15 2003 Return-Path: Delivered-To: freebsd-arch@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id D9E5737B401 for ; Sun, 15 Jun 2003 12:02:15 -0700 (PDT) Received: from falcon.midgard.homeip.net (h76n3fls20o913.telia.com [213.67.148.76]) by mx1.FreeBSD.org (Postfix) with SMTP id B38A943FBF for ; Sun, 15 Jun 2003 12:02:12 -0700 (PDT) (envelope-from ertr1013@student.uu.se) Received: (qmail 75511 invoked by uid 1001); 15 Jun 2003 19:02:09 -0000 Date: Sun, 15 Jun 2003 21:02:09 +0200 From: Erik Trulsson To: Don Lewis Message-ID: <20030615190209.GA75458@falcon.midgard.homeip.net> Mail-Followup-To: Don Lewis , iedowse@maths.tcd.ie, freebsd-arch@FreeBSD.org References: <200306151406.aa36218@salmon.maths.tcd.ie> <200306151826.h5FIPvM7046944@gw.catspoiler.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <200306151826.h5FIPvM7046944@gw.catspoiler.org> User-Agent: Mutt/1.5.4i cc: iedowse@maths.tcd.ie cc: freebsd-arch@FreeBSD.org Subject: Re: Message buffer and printf reentrancy patch X-BeenThere: freebsd-arch@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Discussion related to FreeBSD architecture List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 15 Jun 2003 19:02:16 -0000 On Sun, Jun 15, 2003 at 11:25:57AM -0700, Don Lewis wrote: > On 15 Jun, Ian Dowse wrote: > > > > Below is a patch that makes the implementation of the kernel message > > buffer mostly reentrant and more generic, and stops printf() ever > > calling directly into the tty code. This should fix panics that can > > occur via tputchar() when using xconsole, and generally make the > > use of printf() in the kernel a bit safer. Many of the ideas here > > were suggested by Bruce Evans. > > > > A summary of the changes: > > - Use atomic operations to update the message buffer pointers. > > - Use a kind of sequence number for the pointers instead of just > > the offset into the buffer, as this avoids the need for the read > > code to touch the write pointer or the write code to touch the > > read pointer. > > > +#define MSGBUF_SEQNORM(mbp, seq) ((seq) % (mbp)->msg_seqmod + ((seq) < 0 ? \ > > + (mbp)->msg_seqmod : 0)) > > +#define MSGBUF_SEQ_TO_POS(mbp, seq) ((int)((u_int)(seq) % \ > > + (u_int)(mbp)->msg_size)) > > +#define MSGBUF_SEQSUB(mbp, seq1, seq2) (MSGBUF_SEQNORM(mbp, (seq1) - (seq2))) > > + > > According to my copy of K&R, there is no guarantee that ((negative_int % > postive_int) <= 0) on all platforms, though this is generally true. With a C99 compiler it is always true. In C89 it was implementation defined if integer division rounded towards zero or towards negative-infinity. In C99 integer division always rounds towards zero. This combined with the fact that (a/b)*b + a%b == a is always true (for integer a,b and b!=0) means that (neg_int % pos_int <= 0 ) is always true in C99, while it wasn't always true in C89. -- Erik Trulsson ertr1013@student.uu.se