From tlb@anybots.com  Tue Sep 20 19:07:50 2005
Return-Path: <tlb@anybots.com>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id A59D616A41F
	for <FreeBSD-gnats-submit@freebsd.org>; Tue, 20 Sep 2005 19:07:50 +0000 (GMT)
	(envelope-from tlb@anybots.com)
Received: from anybots.com (w047.z064220186.sjc-ca.dsl.cnc.net [64.220.186.47])
	by mx1.FreeBSD.org (Postfix) with SMTP id 5244D43D46
	for <FreeBSD-gnats-submit@freebsd.org>; Tue, 20 Sep 2005 19:07:50 +0000 (GMT)
	(envelope-from tlb@anybots.com)
Received: (qmail 14112 invoked from network); 20 Sep 2005 19:07:49 -0000
Received: from unknown (HELO fester) (10.10.10.20)
  by 10.10.10.20 with SMTP; 20 Sep 2005 19:07:49 -0000
Message-Id: <1127243269.92680.7.camel@lab>
Date: Tue, 20 Sep 2005 12:07:49 -0700
From: Trevor Blackwell <tlb@anybots.com>
To: FreeBSD-gnats-submit@freebsd.org
Subject: i386_set_ioperm doesn't take effect immediately

>Number:         86380
>Category:       i386
>Synopsis:       i386_set_ioperm doesn't take effect immediately
>Confidential:   no
>Severity:       serious
>Priority:       low
>Responsible:    freebsd-i386
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Tue Sep 20 19:10:02 GMT 2005
>Closed-Date:    Sat Jun 23 16:02:16 GMT 2007
>Last-Modified:  Sat Jun 23 16:02:16 GMT 2007
>Originator:     Trevor Blackwell
>Release:        FreeBSD 5.4-RELEASE-p6 i386
>Organization:
Anybots Inc
>Environment:

System: FreeBSD lab.anybots.com 5.4-RELEASE-p6 FreeBSD 5.4-RELEASE-p6 #0: Thu Sep 15 12:26:46 PDT 2005 root@tlb.anybots.com:/usr/obj/usr/src/sys/LAB i386


>Description:

Calling i386_set_ioperm immediately followed by an inb or outb causes a bus error. Inserting a context switch delay between the two
avoids the problem

>How-To-Repeat:

cat >t_ioperm.c <<EOF
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <machine/sysarch.h>
#include <machine/cpufunc.h>

void
test_ioperm(int base, int len, int wr, int delay)
{
  if (i386_set_ioperm(base, len, 1)<0) {
    fprintf(stderr, "Can't set ioperm on %d %d\n", base, len);
    exit(1);
  }
  if (delay) {
    usleep(2000); // FreeBSD 5.4 seems to sometimes get a bus error if we do IO too soon after set_ioperm
  }
  if (wr) {
    outb(base, 0x00);
  } else {
    inb(base);
  }
}

int
main(int argc, char **argv)
{
  test_ioperm(atoi(argv[1]), atoi(argv[2]), atoi(argv[3]), atoi(argv[4]));
  return 0;
}

EOF

gcc -o t_ioperm t_ioperm.c
sudo ./t_ioperm 1018 8 0 0   # expect bus error
sudo ./t_ioperm 1018 8 0 1   # no bus error

(the port number is byte 2 of COM1, a fairly harmless thing to read from)

>Fix:

Inserting a usleep after i386_set_ioperm seems to work around it.
I assume there's some fancy lazy updating going on in the TLB management.
This happens for me on multiple machines, including a dual Xeon, a P4, and an old Pentium-MMX

-- 
Trevor Blackwell      tlb@tlb.org          (650) 776-7870


>Release-Note:
>Audit-Trail:

From: nge@cs.hmc.edu
To: bug-followup@FreeBSD.org, tlb@anybots.com
Cc:  
Subject: Re: i386/86380: i386_set_ioperm doesn't take effect immediately
Date: Tue, 11 Oct 2005 20:14:21 -0700 (PDT)

 This is the same bug as i386/84842, which has been fixed in 5-STABLE, 6.x
 and CURRENT.
 

From: Nate Eldredge <nge@cs.hmc.edu>
To: bug-followup@FreeBSD.org, tlb@anybots.com
Cc:  
Subject: Re: i386/86380: i386_set_ioperm doesn't take effect immediately
Date: Tue, 11 Oct 2005 20:17:20 -0700 (PDT)

 This is the same bug as i386/84842 which has been fixed in 5-STABLE, 6.x 
 and CURRENT.  Close this?
 
 -- 
 Nate Eldredge
 nge@cs.hmc.edu

From: Trevor Blackwell <tlb@anybots.com>
To: Nate Eldredge <nge@cs.hmc.edu>
Cc: bug-followup@FreeBSD.org
Subject: Re: i386/86380: i386_set_ioperm doesn't take effect immediately
Date: Wed, 12 Oct 2005 00:43:13 -0700

 Sure, close it.
 
 On Oct 11, 2005, at 8:17 PM, Nate Eldredge wrote:
 
 > This is the same bug as i386/84842 which has been fixed in 5-STABLE, 
 > 6.x and CURRENT.  Close this?
 >
 > -- 
 > Nate Eldredge
 > nge@cs.hmc.edu
 >
 --
 Trevor Blackwell              650 776 7870           tlb@tlb.org
 
State-Changed-From-To: open->closed 
State-Changed-By: gavin 
State-Changed-When: Sat Jun 23 16:01:50 UTC 2007 
State-Changed-Why:  

Close, on submitters request 

http://www.freebsd.org/cgi/query-pr.cgi?pr=86380 
>Unformatted:
