ioperm.c revision 1.2.2.2 1 1.2.2.2 matt /* $NetBSD: ioperm.c,v 1.2.2.2 2008/01/09 01:37:32 matt Exp $ */
2 1.2.2.2 matt
3 1.2.2.2 matt /*-
4 1.2.2.2 matt * Copyright (c)2008 YAMAMOTO Takashi,
5 1.2.2.2 matt * All rights reserved.
6 1.2.2.2 matt *
7 1.2.2.2 matt * Redistribution and use in source and binary forms, with or without
8 1.2.2.2 matt * modification, are permitted provided that the following conditions
9 1.2.2.2 matt * are met:
10 1.2.2.2 matt * 1. Redistributions of source code must retain the above copyright
11 1.2.2.2 matt * notice, this list of conditions and the following disclaimer.
12 1.2.2.2 matt * 2. Redistributions in binary form must reproduce the above copyright
13 1.2.2.2 matt * notice, this list of conditions and the following disclaimer in the
14 1.2.2.2 matt * documentation and/or other materials provided with the distribution.
15 1.2.2.2 matt *
16 1.2.2.2 matt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.2.2.2 matt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.2.2.2 matt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.2.2.2 matt * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.2.2.2 matt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.2.2.2 matt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.2.2.2 matt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.2.2.2 matt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.2.2.2 matt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.2.2.2 matt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.2.2.2 matt * SUCH DAMAGE.
27 1.2.2.2 matt */
28 1.2.2.2 matt
29 1.2.2.2 matt #include <sys/cdefs.h>
30 1.2.2.2 matt #ifndef lint
31 1.2.2.2 matt __RCSID("$NetBSD: ioperm.c,v 1.2.2.2 2008/01/09 01:37:32 matt Exp $");
32 1.2.2.2 matt #endif /* not lint */
33 1.2.2.2 matt
34 1.2.2.2 matt #include <err.h>
35 1.2.2.2 matt #include <signal.h>
36 1.2.2.2 matt #include <setjmp.h>
37 1.2.2.2 matt #include <stdbool.h>
38 1.2.2.2 matt #include <stdint.h>
39 1.2.2.2 matt #include <stdio.h>
40 1.2.2.2 matt #include <stdlib.h>
41 1.2.2.2 matt #include <string.h>
42 1.2.2.2 matt #include <unistd.h>
43 1.2.2.2 matt
44 1.2.2.2 matt int i386_get_ioperm(unsigned long *);
45 1.2.2.2 matt int i386_set_ioperm(unsigned long *);
46 1.2.2.2 matt
47 1.2.2.2 matt /* arbitrary port to test */
48 1.2.2.2 matt #define IO_TIMER1 0x40
49 1.2.2.2 matt #define TIMER_CNTR0 0
50 1.2.2.2 matt #define PORT (IO_TIMER1+TIMER_CNTR0)
51 1.2.2.2 matt
52 1.2.2.2 matt static uint8_t
53 1.2.2.2 matt inb(uint16_t port)
54 1.2.2.2 matt {
55 1.2.2.2 matt uint8_t data;
56 1.2.2.2 matt
57 1.2.2.2 matt __asm __volatile("inb %1, %0" : "=a"(data) : "id"(port));
58 1.2.2.2 matt return data;
59 1.2.2.2 matt }
60 1.2.2.2 matt
61 1.2.2.2 matt sigjmp_buf env;
62 1.2.2.2 matt int gotsig;
63 1.2.2.2 matt
64 1.2.2.2 matt static void
65 1.2.2.2 matt sighandler(int sig)
66 1.2.2.2 matt {
67 1.2.2.2 matt
68 1.2.2.2 matt siglongjmp(env, sig);
69 1.2.2.2 matt }
70 1.2.2.2 matt
71 1.2.2.2 matt static void
72 1.2.2.2 matt try(const char *msg, bool success)
73 1.2.2.2 matt {
74 1.2.2.2 matt int sig;
75 1.2.2.2 matt
76 1.2.2.2 matt sig = sigsetjmp(env, 1);
77 1.2.2.2 matt if (sig == 0) {
78 1.2.2.2 matt inb(PORT);
79 1.2.2.2 matt if (!success) {
80 1.2.2.2 matt errx(EXIT_FAILURE, "%s: unexpected success of inb",
81 1.2.2.2 matt msg);
82 1.2.2.2 matt }
83 1.2.2.2 matt return;
84 1.2.2.2 matt }
85 1.2.2.2 matt if (success) {
86 1.2.2.2 matt errx(EXIT_FAILURE, "%s: got signal %d\n", msg, sig);
87 1.2.2.2 matt }
88 1.2.2.2 matt }
89 1.2.2.2 matt
90 1.2.2.2 matt int
91 1.2.2.2 matt main(int argc, char *argv[])
92 1.2.2.2 matt {
93 1.2.2.2 matt unsigned long buf[32];
94 1.2.2.2 matt unsigned long buf2[32];
95 1.2.2.2 matt int ret;
96 1.2.2.2 matt int i;
97 1.2.2.2 matt
98 1.2.2.2 matt ret = i386_get_ioperm(buf);
99 1.2.2.2 matt if (ret == -1) {
100 1.2.2.2 matt err(EXIT_FAILURE, "get_ioperm 1");
101 1.2.2.2 matt }
102 1.2.2.2 matt for (i = 0; i < __arraycount(buf); i++) {
103 1.2.2.2 matt if (buf[i] != 0xffffffff) {
104 1.2.2.2 matt errx(EXIT_FAILURE, "buf[%d] == 0x%lx", i, buf[i]);
105 1.2.2.2 matt }
106 1.2.2.2 matt }
107 1.2.2.2 matt
108 1.2.2.2 matt signal(SIGSEGV, sighandler);
109 1.2.2.2 matt
110 1.2.2.2 matt try("1", false);
111 1.2.2.2 matt sleep(1);
112 1.2.2.2 matt
113 1.2.2.2 matt memset(buf2, 0x00, sizeof(buf2));
114 1.2.2.2 matt strcpy((char *)buf2, "foobarbaz");
115 1.2.2.2 matt buf2[PORT / 8 / sizeof(unsigned long)] &=
116 1.2.2.2 matt ~(1 << (PORT % (8 * sizeof(unsigned long))));
117 1.2.2.2 matt ret = i386_set_ioperm(buf2);
118 1.2.2.2 matt if (ret == -1) {
119 1.2.2.2 matt err(EXIT_FAILURE, "set_ioperm 1");
120 1.2.2.2 matt }
121 1.2.2.2 matt ret = i386_get_ioperm(buf);
122 1.2.2.2 matt if (ret == -1) {
123 1.2.2.2 matt err(EXIT_FAILURE, "get_ioperm 2");
124 1.2.2.2 matt }
125 1.2.2.2 matt if (memcmp(buf, buf2, sizeof(buf))) {
126 1.2.2.2 matt errx(EXIT_FAILURE, "iomap mismatch");
127 1.2.2.2 matt }
128 1.2.2.2 matt
129 1.2.2.2 matt sleep(1);
130 1.2.2.2 matt try("2", true);
131 1.2.2.2 matt sleep(1);
132 1.2.2.2 matt
133 1.2.2.2 matt buf2[PORT / 8 / sizeof(unsigned long)] |=
134 1.2.2.2 matt (1 << (PORT % (8 * sizeof(unsigned long))));
135 1.2.2.2 matt ret = i386_set_ioperm(buf2);
136 1.2.2.2 matt if (ret == -1) {
137 1.2.2.2 matt err(EXIT_FAILURE, "set_ioperm 3");
138 1.2.2.2 matt }
139 1.2.2.2 matt
140 1.2.2.2 matt sleep(1);
141 1.2.2.2 matt try("3", false);
142 1.2.2.2 matt }
143