xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/iopl/iopl01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) Linux Test Project, 2003-2024
4  * Copyright (c) Wipro Technologies Ltd, 2002
5  * Author: Subhab Biswas <[email protected]>
6  */
7 
8 /*\
9  * [Description]
10  *
11  * This is a basic test for iopl(2) system call.
12  *
13  * Test the system call for possible privilege levels.
14  * As the privilege level for a normal process is 0, start by
15  * setting/changing the level to 0.
16  */
17 
18 #include <errno.h>
19 #include <unistd.h>
20 
21 #include "tst_test.h"
22 
23 #if defined __i386__ || defined(__x86_64__)
24 #include <sys/io.h>
25 
verify_iopl(void)26 static void verify_iopl(void)
27 {
28 	int total_level = 4;
29 	int level;
30 
31 	for (level = 0; level < total_level; ++level) {
32 
33 		TEST(iopl(level));
34 
35 		if (TST_RET == -1) {
36 			tst_res(TFAIL | TTERRNO, "iopl() failed for level %d, "
37 					"errno=%d : %s", level,
38 					TST_ERR, tst_strerrno(TST_ERR));
39 		} else {
40 			tst_res(TPASS, "iopl() passed for level %d, "
41 					"returned %ld", level, TST_RET);
42 		}
43 	}
44 }
45 
cleanup(void)46 static void cleanup(void)
47 {
48 	/*
49 	 * back to I/O privilege for normal process.
50 	 */
51 	if (iopl(0) == -1)
52 		tst_res(TWARN, "iopl() cleanup failed");
53 }
54 
55 static struct tst_test test = {
56 	.test_all = verify_iopl,
57 	.needs_root = 1,
58 	/* iopl() is restricted under kernel lockdown. */
59 	.skip_in_lockdown = 1,
60 	.cleanup = cleanup,
61 };
62 
63 #else
64 TST_TEST_TCONF("LSB v1.3 does not specify iopl() for this architecture. (only for i386 or x86_64)");
65 #endif /* __i386_, __x86_64__*/
66