public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Stephen Hemminger <stephen@networkplumber.org>
To: Joerg Roedel <joro@8bytes.org>
Cc: kvm@vger.kernel.org
Subject: I/O port permission bit inheritance between threads
Date: Mon, 20 May 2013 14:24:31 -0700	[thread overview]
Message-ID: <20130520142431.5221e2d0@nehalam.linuxnetplumber.net> (raw)

ioperm() inheritance across threads is different in KVM then when run
on physical hardware.  The following program runs on physical hardware
but get SEGV under KVM.

It appears that the I/O permission bits are not shared between threads
in the same way.

/* Original Copyright 2011, Kees Cook <kees@outflux.net>, License: GPLv2 */
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
#include <sys/io.h>

static void *beep(void *arg)
{
	unsigned char bits;

	fprintf(stderr, "waiting\n");
	sleep(1);

	fprintf(stderr, "beeping\n");
	/* turn on speaker */
	bits = inb(0x61);
	bits |= 3;
	outb(bits, 0x61);

	/* set 1000 Hz frequency */
	bits = 0xA9;
	outb(bits, 0x42);
	bits = 0x04;
	outb(bits, 0x42);

	/* listen to the beep */
	sleep(4);
	fprintf(stderr, "done\n");

	return NULL;
}

int main() {
	pthread_t tid;
	unsigned char orig;

	if (pthread_create(&tid, NULL, &beep, NULL)) {
		perror("pthread");
		return 1;
	}

	/* gain access to speaker control port */
	if (ioperm(0x61, 0x61, 1) < 0) {
		perror("0x61");
		return 1;
	}
	orig = inb(0x61);

	/* gain access to speaker frequency port */
	if (ioperm(0x42, 0x42, 1) < 0) {
		perror("0x42");
		return 2;
	}

	fprintf(stderr, "joining\n");
	pthread_join(tid, NULL);
	
	/* restore speaker bits to turn off speaker */
	outb(orig, 0x61);
	fprintf(stderr, "done\n");
	return 0;
}

             reply	other threads:[~2013-05-20 21:24 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-05-20 21:24 Stephen Hemminger [this message]
2013-05-21  9:50 ` I/O port permission bit inheritance between threads Joerg Roedel
2013-05-21 10:01   ` Gleb Natapov
2013-05-21 15:27     ` Stephen Hemminger
2013-05-23  9:27       ` Gleb Natapov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20130520142431.5221e2d0@nehalam.linuxnetplumber.net \
    --to=stephen@networkplumber.org \
    --cc=joro@8bytes.org \
    --cc=kvm@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox