LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 12-04-2012, 09:09 AM   #1
CelticFiddler
LQ Newbie
 
Registered: Aug 2012
Location: Plano, TX
Distribution: Debian 6
Posts: 11

Rep: Reputation: Disabled
What causes a segmentation fault on a call to inb()?


I am getting a segmentation fault when trying to read a port with inb_p( ). I'm compiling this on a Debian system running 2.6.6 kernel on an Intel D525 dual-core system (Advantech PCM 9389 SBC). Here is a sample program which illustrates the segfault.

Code:
/*
 * ioexample.c: very simple ioexample of port I/O
 * very simple port i/o
 * Compile with `gcc -O2 -o ioexample ioexample.c',
 * and run as root with `./ioexample'.
 */
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/io.h>

#define BASEPORT 0x0100 /* iobase for sample system */
#define FLIPC 0x01
#define FLIPST 0x0
#define DIPSWITCH 0x25

int main()
{
char cinput;

  cinput = 0xff;
  setuid(0);
  printf("begin\n");
  /* Get access to the ports */
  if (ioperm(BASEPORT+DIPSWITCH, 10, 1)) 
  {
     perror("ioperm");
     exit(EXIT_FAILURE);
  }

  printf("read the dipswitch with pause\n");
  cinput = inb_p(BASEPORT+DIPSWITCH); // <=====SEGFAULT HERE

  /* We don't need the ports anymore */
  if (ioperm(BASEPORT+DIPSWITCH, 10, 0))
  {
     perror("ioperm");
     exit(EXIT_FAILURE);
  }

  printf("Dipswitch setting: 0x%X", cinput); 
  exit(EXIT_SUCCESS);
}

/* end of ioexample.c */
What is the probable cause? How do I fix this?

Currently, I don't have any devices hooked up. Could this cause the segfault? I would have expected to get either a zero or some random byte, but not a segfault.

Other things I tried:
1) Declared the input variable as int instead of char.
2) Used iopl() instead of ioperm()
3) /proc/ioports did not list anything at address 0x100, so I tried several other port addresses that were listed, with the same result. Then I decided to try an output to a known parallel port location (0x0378), and outb did not cause a segfault. However, trying to read either 0x378 or 0x379 did cause a segfault. I am beginning to suspect that the problem is hardware related.

And yes, I'm running this as root.
 
Old 12-04-2012, 05:21 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 1,479

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Make sure selinux is either not enabled or allows the operations.

You need to enable access to port 80 if you use the _p version (pause). Otherwise just use inb.
 
1 members found this post helpful.
Old 12-05-2012, 11:34 AM   #3
CelticFiddler
LQ Newbie
 
Registered: Aug 2012
Location: Plano, TX
Distribution: Debian 6
Posts: 11

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by smallpond View Post
Make sure selinux is either not enabled or allows the operations.

You need to enable access to port 80 if you use the _p version (pause). Otherwise just use inb.
Calling ioperm for 0x80 did the trick. Apparently, when I tried iopl(), I did not call it correctly, because that should have worked.

Thanks!
 
  


Reply

Tags
low-level, port, segfault, segmentation fault


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
_int_free() call crashes in gtk/pango lib (segmentation fault), what is the reason benni1 Programming 2 05-08-2009 08:36 AM
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika Suse/Novell 2 12-02-2005 09:34 AM
Segmentation fault alnreddy Linux - Software 1 11-05-2005 08:54 PM
segmentation fault vibhory2j Programming 7 10-16-2005 07:06 AM
segmentation fault w/ c++ function call wsimmons Programming 3 10-16-2001 02:06 AM


All times are GMT -5. The time now is 02:17 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration