From: Stephen Hemminger Subject: I/O port permission bit inheritance between threads Date: Mon, 20 May 2013 14:24:31 -0700 Message-ID: <20130520142431.5221e2d0@nehalam.linuxnetplumber.net> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Cc: kvm@vger.kernel.org To: Joerg Roedel Return-path: Received: from mail-pd0-f180.google.com ([209.85.192.180]:34950 "EHLO mail-pd0-f180.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1758267Ab3ETVYf (ORCPT ); Mon, 20 May 2013 17:24:35 -0400 Received: by mail-pd0-f180.google.com with SMTP id 10so4651284pdc.25 for ; Mon, 20 May 2013 14:24:35 -0700 (PDT) Sender: kvm-owner@vger.kernel.org List-ID: 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 , License: GPLv2 */ #include #include #include #include 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; }