diff options
author | Alex Waterman <alexw@nvidia.com> | 2018-09-07 19:19:22 -0400 |
---|---|---|
committer | mobile promotions <svcmobile_promotions@nvidia.com> | 2018-09-20 18:14:51 -0400 |
commit | ec067c5ed1f00517dbd771fbe9809d2340ec908b (patch) | |
tree | 9914e1022dce6fb25e8dc87a8253d09371e690e0 /userspace/units/posix-env/posix-env.c | |
parent | c1b66bc6a93eeecec1aea0e5bdf93397d0458823 (diff) |
gpu: nvgpu: Add POSIX sanity unit test
Add a unit test to do sanity checks for the environment. These
checks currently include:
- Size of types (f.e. u32 is really 4 bytes)
- Signedness of types (u32 is really unsigned)
- Endianness check
- Pointers fit in u64s
Note: the endianness check does not fail for either detected
endianness. It just prints the determined endianness. The exception
to this is if the check itself is broken and does not successfully
determine what endianness the underlying machine is. In that case
the test fails. Unless the underlying architure is a so called
middle endian machine something is horribly wrong.
We will determine what to actually do about this endianness
check once we determine what we need to do with BIOS fields,
etc. We proabbly don't really care what the machine endianness is
but we do need to make sure that we access data that may not match
machine endianness correctly.
JIRA NVGPU-1039
Change-Id: I5be68cf4dcea87e9e746262fcc0372380ef57df4
Signed-off-by: Alex Waterman <alexw@nvidia.com>
Reviewed-on: https://git-master.nvidia.com/r/1816897
Reviewed-by: svc-misra-checker <svc-misra-checker@nvidia.com>
GVS: Gerrit_Virtual_Submit
Reviewed-by: Terje Bergstrom <tbergstrom@nvidia.com>
Reviewed-by: mobile promotions <svcmobile_promotions@nvidia.com>
Tested-by: mobile promotions <svcmobile_promotions@nvidia.com>
Diffstat (limited to 'userspace/units/posix-env/posix-env.c')
-rw-r--r-- | userspace/units/posix-env/posix-env.c | 198 |
1 files changed, 198 insertions, 0 deletions
diff --git a/userspace/units/posix-env/posix-env.c b/userspace/units/posix-env/posix-env.c new file mode 100644 index 00000000..fc090352 --- /dev/null +++ b/userspace/units/posix-env/posix-env.c | |||
@@ -0,0 +1,198 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. | ||
3 | * | ||
4 | * Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | * copy of this software and associated documentation files (the "Software"), | ||
6 | * to deal in the Software without restriction, including without limitation | ||
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | * and/or sell copies of the Software, and to permit persons to whom the | ||
9 | * Software is furnished to do so, subject to the following conditions: | ||
10 | * | ||
11 | * The above copyright notice and this permission notice shall be included in | ||
12 | * all copies or substantial portions of the Software. | ||
13 | * | ||
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | * DEALINGS IN THE SOFTWARE. | ||
21 | */ | ||
22 | |||
23 | #include <stdlib.h> | ||
24 | #include <stdint.h> | ||
25 | |||
26 | #include <unit/io.h> | ||
27 | #include <unit/unit.h> | ||
28 | |||
29 | #include <nvgpu/types.h> | ||
30 | |||
31 | /* | ||
32 | * Test if the passed type is signed. In the signed case this expression becomes | ||
33 | * | ||
34 | * x = 0; | ||
35 | * y = x - 1 = -1 | ||
36 | * | ||
37 | * And -1 < 0 will be true. In the unsigned case, we rely on wrap around. We | ||
38 | * have the following | ||
39 | * | ||
40 | * x = 0U | ||
41 | * y = x - 1 = ~0 (i.e <TYPE>_MAX) | ||
42 | * | ||
43 | * Thus the expression y < x is false. | ||
44 | */ | ||
45 | #define IS_SIGNED_TYPE(__type__) \ | ||
46 | ({ \ | ||
47 | __type__ x = (__type__)0; \ | ||
48 | __type__ y = x - (__type__)1; \ | ||
49 | y < x; \ | ||
50 | }) | ||
51 | |||
52 | /* | ||
53 | * Ensure that our sized types are of the correct size. Assumes an 8bit byte of | ||
54 | * course. | ||
55 | */ | ||
56 | static int sanity_test_sizes(struct unit_module *m, | ||
57 | struct gk20a *g, void *args) | ||
58 | { | ||
59 | size_t size_u8 = sizeof(u8); | ||
60 | size_t size_u16 = sizeof(u16); | ||
61 | size_t size_u32 = sizeof(u32); | ||
62 | size_t size_u64 = sizeof(u64); | ||
63 | size_t size_s8 = sizeof(s8); | ||
64 | size_t size_s16 = sizeof(s16); | ||
65 | size_t size_s32 = sizeof(s32); | ||
66 | size_t size_s64 = sizeof(s64); | ||
67 | |||
68 | /* Unsigned. */ | ||
69 | if (size_u8 != 1U) | ||
70 | unit_return_fail(m, | ||
71 | "sizeof(u8) != 1 byte! (Actual size: %zu)\n", | ||
72 | size_u8); | ||
73 | if (size_u16 != 2U) | ||
74 | unit_return_fail(m, | ||
75 | "sizeof(u16) != 2 bytes! (Actual size: %zu)\n", | ||
76 | size_u16); | ||
77 | if (size_u32 != 4U) | ||
78 | unit_return_fail(m, | ||
79 | "sizeof(u32) != 4 bytes! (Actual size: %zu)\n", | ||
80 | size_u32); | ||
81 | if (size_u64 != 8U) | ||
82 | unit_return_fail(m, | ||
83 | "sizeof(u64) != 8 bytes! (Actual size: %zu)\n", | ||
84 | size_u64); | ||
85 | |||
86 | /* Signed. */ | ||
87 | if (size_s8 != 1U) | ||
88 | unit_return_fail(m, | ||
89 | "sizeof(s8) != 1 byte! (Actual size: %zu)\n", | ||
90 | size_s8); | ||
91 | if (size_s16 != 2U) | ||
92 | unit_return_fail(m, | ||
93 | "sizeof(s16) != 2 bytes! (Actual size: %zu)\n", | ||
94 | size_s16); | ||
95 | if (size_s32 != 4U) | ||
96 | unit_return_fail(m, | ||
97 | "sizeof(s32) != 4 bytes! (Actual size: %zu)\n", | ||
98 | size_s32); | ||
99 | if (size_s64 != 8U) | ||
100 | unit_return_fail(m, | ||
101 | "sizeof(s64) != 8 bytes! (Actual size: %zu)\n", | ||
102 | size_s64); | ||
103 | |||
104 | return UNIT_SUCCESS; | ||
105 | } | ||
106 | |||
107 | /* | ||
108 | * Make sure that the signed types really are signed and that the unsigned types | ||
109 | * really are not. | ||
110 | */ | ||
111 | static int sanity_test_signage(struct unit_module *m, | ||
112 | struct gk20a *g, void *args) | ||
113 | { | ||
114 | |||
115 | if (!IS_SIGNED_TYPE(s8)) | ||
116 | unit_return_fail(m, "s8 is not signed!\n"); | ||
117 | if (!IS_SIGNED_TYPE(s16)) | ||
118 | unit_return_fail(m, "s16 is not signed!\n"); | ||
119 | if (!IS_SIGNED_TYPE(s32)) | ||
120 | unit_return_fail(m, "s32 is not signed!\n"); | ||
121 | if (!IS_SIGNED_TYPE(s64)) | ||
122 | unit_return_fail(m, "s64 is not signed!\n"); | ||
123 | |||
124 | if (IS_SIGNED_TYPE(u8)) | ||
125 | unit_return_fail(m, "u8 is signed!\n"); | ||
126 | if (IS_SIGNED_TYPE(u16)) | ||
127 | unit_return_fail(m, "u16 is signed!\n"); | ||
128 | if (IS_SIGNED_TYPE(u32)) | ||
129 | unit_return_fail(m, "u32 is signed!\n"); | ||
130 | if (IS_SIGNED_TYPE(u64)) | ||
131 | unit_return_fail(m, "u64 is signed!\n"); | ||
132 | |||
133 | return UNIT_SUCCESS; | ||
134 | } | ||
135 | |||
136 | /* | ||
137 | * Ensure that a u64 can hold a pointer since in some places we use a u64 to | ||
138 | * pass back a pointer value. | ||
139 | */ | ||
140 | static int sanity_test_ptr_in_u64(struct unit_module *m, | ||
141 | struct gk20a *g, void *args) | ||
142 | { | ||
143 | if (sizeof(u64) < sizeof(uintptr_t)) | ||
144 | unit_return_fail(m, | ||
145 | "u64 size (%zu b) less than pointer size (%zu b)", | ||
146 | sizeof(u64), sizeof(uintptr_t)); | ||
147 | |||
148 | return UNIT_SUCCESS; | ||
149 | } | ||
150 | |||
151 | static int sanity_test_endianness(struct unit_module *m, | ||
152 | struct gk20a *g, void *args) | ||
153 | { | ||
154 | u32 i; | ||
155 | u32 x = 0x12345678; | ||
156 | u8 *ptr_x_u8 = (u8 *)&x; | ||
157 | u16 *ptr_x_u16 = (u16 *)&x; | ||
158 | |||
159 | /* | ||
160 | * Print what endianness we have. For now we have not explicitly decided | ||
161 | * to support one or the other, but this will have to be determined | ||
162 | * eventually. | ||
163 | * | ||
164 | * We have just been lucky so far. | ||
165 | */ | ||
166 | unit_info(m, "u32 x = 0x%x\n", x); | ||
167 | for (i = 0; i < sizeof(u32) / sizeof(u16); i++) { | ||
168 | unit_info(m, " &x + %zu: 0x%04hx\n", | ||
169 | i * sizeof(u16), ptr_x_u16[i]); | ||
170 | } | ||
171 | |||
172 | unit_info(m, "u32 x = 0x%x\n", x); | ||
173 | for (i = 0; i < sizeof(u32); i++) { | ||
174 | unit_info(m, " &x + %u: 0x%02hhx\n", i, ptr_x_u8[i]); | ||
175 | } | ||
176 | |||
177 | switch (ptr_x_u8[0]) { | ||
178 | case 0x12: | ||
179 | unit_info(m, "Machine endianness: big\n"); | ||
180 | break; | ||
181 | case 0x78: | ||
182 | unit_info(m, "Machine endianness: little\n"); | ||
183 | break; | ||
184 | default: | ||
185 | unit_return_fail(m, "Machine endianness: middle/unknown ??\n"); | ||
186 | } | ||
187 | |||
188 | return UNIT_SUCCESS; | ||
189 | } | ||
190 | |||
191 | struct unit_module_test posix_env_tests[] = { | ||
192 | UNIT_TEST(sizes, sanity_test_sizes, NULL), | ||
193 | UNIT_TEST(signage, sanity_test_signage, NULL), | ||
194 | UNIT_TEST(endianness, sanity_test_endianness, NULL), | ||
195 | UNIT_TEST(ptr_in_u64, sanity_test_ptr_in_u64, NULL), | ||
196 | }; | ||
197 | |||
198 | UNIT_MODULE(posix_env, posix_env_tests, UNIT_PRIO_POSIX_TEST); | ||