[PATCH] Add compatibility <sys/io.h>

Robert Millan rmh at freebsd.org
Fri Mar 9 21:36:50 UTC 2012


This patch adds a compatibility <sys/io.h> that would make it easier
to build on FreeBSD software that has been written for glibc (see
example1.c).

The functionality in <sys/io.h> is more or less equivalent to
<machine/cpufunc.h> but provides some declarations which are
incompatible with those in <machine/cpufunc.h>. One of these cases is
very unfortunate because it alters I/O semantics, it applies to
outb(), outw() and outl():

  FreeBSD code: outw(port, data);
  Glibc code: outw(data, port);

The undefined I/O behaviour that could potentially result from this is
very scary. Because of this I've written a few checks to prevent both
headers from being used at the same time. Overall, aside from the
portability benefit, my proposed addition has some less obvious
side-effects:

     Desireable side-effect: Adding <sys/io.h> would break
buildability of code that attempts to use both headers at the same
time _WITHOUT_ changing the outw() call semantics (see example2.c).

     Undesireable side-effect: Adding <sys/io.h> would break
buildability of code that attempts to use both headers at the same
time, and was otherwise fine due to conditionalized outw() calls (see
example3.c [1]).

[1] I found a real case where this happens, in
sane-backends-1.0.22/backend/umax_pp_low.c. In case my patch is
accepted, I commit to provide a fix for sane-backends and also to
adjust Debian GNU/kFreeBSD headers to follow the same route [2].

[2] This means: reenable outb(), outw() and outl() in
<machine/cpufunc.h>. Currently we provide both headers, but outb(),
outw() and outl() are entirely disabled. I think it's better for both
projects to provide <sys/io.h> since the majority of code out there is
written with GNU/Linux in mind.

Attached patch is tested against HEAD with "make universe".

-- 
Robert Millan
-------------- next part --------------
Index: sys/amd64/include/cpufunc.h
===================================================================
--- sys/amd64/include/cpufunc.h	(revision 232404)
+++ sys/amd64/include/cpufunc.h	(working copy)
@@ -36,6 +36,10 @@
  * used in preference to this.
  */
 
+#ifdef _SYS_IO_H_
+#error definitions in this file conflict with those in sys/io.h
+#endif
+
 #ifndef _MACHINE_CPUFUNC_H_
 #define	_MACHINE_CPUFUNC_H_
 
Index: sys/i386/include/cpufunc.h
===================================================================
--- sys/i386/include/cpufunc.h	(revision 232404)
+++ sys/i386/include/cpufunc.h	(working copy)
@@ -35,6 +35,10 @@
  * used in preference to this.
  */
 
+#ifdef _SYS_IO_H_
+#error definitions in this file conflict with those in sys/io.h
+#endif
+
 #ifndef _MACHINE_CPUFUNC_H_
 #define	_MACHINE_CPUFUNC_H_
 
Index: sys/sys/io.h
===================================================================
--- sys/sys/io.h	(revision 0)
+++ sys/sys/io.h	(revision 0)
@@ -0,0 +1,58 @@
+/*-
+ * Copyright (c) 2012 Robert Millan
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+/*
+ * This file provides compatibility glue for code that expects GNU-style
+ * sys/io.h. Code written for FreeBSD should use sys/systm.h instead.
+ */
+
+#ifdef _MACHINE_CPUFUNC_H_
+#error definitions in this file conflict with those in machine/cpufunc.h
+#endif
+
+#ifndef _SYS_IO_H_
+
+#if !defined(__i386__) && !defined(__amd64__)
+#error this file is only useful on i386 and amd64
+#endif
+
+#include <sys/cdefs.h>
+
+#define outb	bsd_outb
+#define outw	bsd_outw
+#define outl	bsd_outl
+#include <machine/cpufunc.h>
+#undef outb
+#undef outw
+#undef outl
+
+#define outb(data,port)		bsd_outb(port,data)
+#define outw(data,port)		bsd_outw(port,data)
+#define outl(data,port)		bsd_outl(port,data)
+
+#define _SYS_IO_H_
+#endif /* !_SYS_IO_H_ */


More information about the freebsd-arch mailing list