• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Copyright (c) Linux Test Project, 2020
4  *  Copyright (c) Wipro Technologies Ltd, 2002
5  */
6 
7 /*
8  * This is a basic test for ioperm(2) system call.
9  * It is intended to provide a limited exposure of the system call.
10  *
11  * Author: Subhab Biswas <subhabrata.biswas@wipro.com>
12  */
13 
14 #include <errno.h>
15 #include <unistd.h>
16 
17 #include "tst_test.h"
18 
19 #if defined __i386__ || defined(__x86_64__)
20 #include <sys/io.h>
21 
22 unsigned long io_addr;
23 #define NUM_BYTES 3
24 #ifndef IO_BITMAP_BITS
25 #define IO_BITMAP_BITS 1024
26 #endif
27 
verify_ioperm(void)28 static void verify_ioperm(void)
29 {
30 	TEST(ioperm(io_addr, NUM_BYTES, 1));
31 
32 	if (TST_RET == -1) {
33 		tst_res(TFAIL | TTERRNO, "ioperm() failed for port address "
34 				"%lu,  errno=%d : %s", io_addr,
35 				TST_ERR, tst_strerrno(TST_ERR));
36 	} else {
37 		tst_res(TPASS, "ioperm() passed for port "
38 				"address %lu, returned %lu",
39 				io_addr, TST_RET);
40 	}
41 }
42 
setup(void)43 static void setup(void)
44 {
45 	/*
46 	 * The value of IO_BITMAP_BITS (include/asm-i386/processor.h) changed
47 	 * from kernel 2.6.8 to permit 16-bits ioperm
48 	 *
49 	 * Ricky Ng-Adam, rngadam@yahoo.com
50 	 * */
51 	if (tst_kvercmp(2, 6, 8) < 0)
52 		io_addr = IO_BITMAP_BITS - NUM_BYTES;
53 	else
54 		io_addr = IO_BITMAP_BITS - NUM_BYTES;
55 }
56 
cleanup(void)57 static void cleanup(void)
58 {
59 	/*
60 	 * Reset I/O privileges for the specified port.
61 	 */
62 	if ((ioperm(io_addr, NUM_BYTES, 0)) == -1)
63 		tst_brk(TBROK | TERRNO, "ioperm() cleanup failed");
64 }
65 
66 static struct tst_test test = {
67 	.test_all = verify_ioperm,
68 	.needs_root = 1,
69 	/* ioperm() is restricted under kernel lockdown. */
70 	.skip_in_lockdown = 1,
71 	.setup = setup,
72 	.cleanup = cleanup,
73 };
74 
75 #else
76 TST_TEST_TCONF("LSB v1.3 does not specify ioperm() for this architecture. (only for i386 or x86_64)");
77 #endif /* __i386_, __x86_64__*/
78