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 iopl(2) system call.
9 *
10 * Test the system call for possible privelege levels.
11 * As the privelge level for a normal process is 0, start by
12 * setting/changing the level to 0.
13 *
14 * Author: Subhab Biswas <subhabrata.biswas@wipro.com>
15 */
16
17 #include <errno.h>
18 #include <unistd.h>
19
20 #include "tst_test.h"
21
22 #if defined __i386__ || defined(__x86_64__)
23 #include <sys/io.h>
24
verify_iopl(void)25 static void verify_iopl(void)
26 {
27 int total_level = 4;
28 int level;
29
30 for (level = 0; level < total_level; ++level) {
31
32 TEST(iopl(level));
33
34 if (TST_RET == -1) {
35 tst_res(TFAIL | TTERRNO, "iopl() failed for level %d, "
36 "errno=%d : %s", level,
37 TST_ERR, tst_strerrno(TST_ERR));
38 } else {
39 tst_res(TPASS, "iopl() passed for level %d, "
40 "returned %ld", level, TST_RET);
41 }
42 }
43 }
44
setup(void)45 static void setup(void)
46 {
47 /* iopl() is restricted under kernel lockdown. */
48 if (tst_lockdown_enabled())
49 tst_brk(TCONF, "Kernel is locked down, skip this test");
50 }
51
cleanup(void)52 static void cleanup(void)
53 {
54 /*
55 * back to I/O privilege for normal process.
56 */
57 if (iopl(0) == -1)
58 tst_res(TWARN, "iopl() cleanup failed");
59 }
60
61 static struct tst_test test = {
62 .test_all = verify_iopl,
63 .needs_root = 1,
64 .setup = setup,
65 .cleanup = cleanup,
66 };
67
68 #else
69 TST_TEST_TCONF("LSB v1.3 does not specify iopl() for this architecture. (only for i386 or x86_64)");
70 #endif /* __i386_, __x86_64__*/
71