1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
|
/* IBM_PROLOG_BEGIN_TAG */
/* This is an automatically generated prolog. */
/* */
/* $Source: src/usr/secureboot/smf/test/testsmf.H $ */
/* */
/* OpenPOWER HostBoot Project */
/* */
/* Contributors Listed Below - COPYRIGHT 2018,2019 */
/* [+] International Business Machines Corp. */
/* */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); */
/* you may not use this file except in compliance with the License. */
/* You may obtain a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or */
/* implied. See the License for the specific language governing */
/* permissions and limitations under the License. */
/* */
/* IBM_PROLOG_END_TAG */
#ifndef __TEST_SMF_H
#define __TEST_SMF_H
#include <cxxtest/TestSuite.H>
#include <errl/errlmanager.H>
#include <targeting/common/target.H>
#include <targeting/common/targetservice.H>
#include <targeting/common/utilFilter.H>
#include <hbotcompid.H>
#include <secureboot/smf.H>
#include <secureboot/secure_reasoncodes.H>
#include <errl/hberrltypes.H>
#include <vfs/vfs.H>
const uint64_t DISTRIBUTE_EXACT_SMF_AMT = 1 * GIGABYTE;
const uint64_t DISTRIBUTE_NON_EXACT_SMF_AMT = 800 * MEGABYTE;
class SmfMemDistributionSuite : public CxxTest::TestSuite
{
private:
bool cv_isSmfLoaded;
/**
* @brief Helper function to set master proc's ATTR_PROC_SMF_BAR_SIZE
* to the desired size passed in as an argument.
* @param[in] i_desiredMemSize the desired size of ATTR_PROC_SMF_BAR_SIZE
* @return nullptr on success; non-nullptr on error
*/
errlHndl_t setSmfMemSize(const uint64_t i_desiredMemSize) const
{
errlHndl_t l_errl = nullptr;
do{
TARGETING::Target* l_masterProc = nullptr;
l_errl = TARGETING::targetService()
.queryMasterProcChipTargetHandle(l_masterProc);
if(l_errl)
{
break;
}
l_masterProc->
setAttr<TARGETING::ATTR_PROC_SMF_BAR_SIZE>(i_desiredMemSize);
}while(0);
return l_errl;
}
/**
* @brief Helper function to fetch the ATTR_PROC_SMF_BAR_SIZE attr from
* the master proc.
* @param[out] o_size the size of ATTR_PROC_SMF_BAR_SIZE on master proc
* @return nullptr on success; non-nullptr on error
*/
errlHndl_t getSmfMemSize(uint64_t& o_size) const
{
errlHndl_t l_errl = nullptr;
do {
TARGETING::Target* l_masterProc = nullptr;
l_errl = TARGETING::targetService()
.queryMasterProcChipTargetHandle(l_masterProc);
if(l_errl)
{
break;
}
o_size = l_masterProc->
getAttr<TARGETING::ATTR_PROC_SMF_BAR_SIZE>();
} while(0);
return l_errl;
}
// Exception: An input of 1 will result in output of 1
uint64_t getHighestPwrTwoLessThan(const uint64_t i_val) const
{
uint64_t l_result = 1;
if(i_val)
{
for(; (l_result << 1) < i_val; l_result <<= 1);
}
else
{
l_result = 0;
}
return l_result;
}
public:
SmfMemDistributionSuite() : cv_isSmfLoaded(false)
{
errlHndl_t l_errl = nullptr;
l_errl = VFS::module_load("libsmf.so");
if(l_errl)
{
TS_FAIL("SmfMemDistributionSuite(): could not load libsmf.so");
errlCommit(l_errl, CXXTEST_COMP_ID);
cv_isSmfLoaded = false;
}
else
{
cv_isSmfLoaded = true;
}
}
~SmfMemDistributionSuite()
{
if(cv_isSmfLoaded)
{
errlHndl_t l_errl = nullptr;
l_errl = VFS::module_unload("libsmf.so");
if(l_errl)
{
TS_FAIL("~SmfMemDistributionSuite(): could not unload libsmf.so");
errlCommit(l_errl, CXXTEST_COMP_ID);
}
}
}
void testDistributeZeroMem() const
{
TS_INFO(ENTER_MRK"testDistributeZeroMem");
errlHndl_t l_errl = nullptr;
do {
if(!cv_isSmfLoaded)
{
break;
}
// Set up SMF on the system target for this test.
// We expect SMF to be turned off when the function runs.
TARGETING::Target* l_sys = nullptr;
TARGETING::targetService().getTopLevelTarget(l_sys);
assert(l_sys != nullptr, "The top level target is nullptr!");
l_sys->setAttr<TARGETING::ATTR_SMF_ENABLED>(true);
// Set SMF mem size to be something other than 0
l_errl = setSmfMemSize(0x1);
if(l_errl)
{
TS_FAIL("testDistributeZeroMem: could not setSmfMemSize");
errlCommit(l_errl, CXXTEST_COMP_ID);
break;
}
l_errl = SECUREBOOT::SMF::distributeSmfMem(0);
if(l_errl)
{
TS_FAIL("testDistributeZeroMem: distributeSmfMem returned an errl. Errl plid = 0x%x", l_errl->plid());
errlCommit(l_errl, CXXTEST_COMP_ID);
}
if(l_sys->getAttr<TARGETING::ATTR_SMF_ENABLED>())
{
// SMF should have been turned OFF with 0 mem request
TS_FAIL("testDistributeZeroMem: SMF should have been disabled on 0 SMF mem request");
}
uint64_t l_resultingSmfSize = 0;
l_errl = getSmfMemSize(l_resultingSmfSize);
if(l_errl)
{
TS_FAIL("testDistributeZeroMem: could not getSmfMemSize");
errlCommit(l_errl, CXXTEST_COMP_ID);
break;
}
if(l_resultingSmfSize != 0)
{
TS_FAIL("testDistributeZeroMem: Request to distribute zero SMF memory resulted in non-zero SMF_BAR_SIZE under master proc");
}
} while(0);
TS_INFO(EXIT_MRK"testDistributeZeroMem");
}
void testDistributeSmallMemAmt() const
{
TS_INFO(INFO_MRK"testDistributeSmallMemAmt");
errlHndl_t l_errl = nullptr;
do {
if(!cv_isSmfLoaded)
{
break;
}
l_errl = SECUREBOOT::SMF::distributeSmfMem(0x1);
if(!l_errl)
{
TS_FAIL("testDistributeSmallMemAmt: distributeSmfMem should have returned an error log.");
}
else
{
if(l_errl->reasonCode() != SECUREBOOT::RC_ALLOCATED_NE_REQUESTED)
{
TS_FAIL("testDistributeSmallMemAmt: Incorrect RC returned. Expected 0x%x, actual 0x%x", SECUREBOOT::RC_ALLOCATED_NE_REQUESTED, l_errl->reasonCode());
if(l_errl->sev() != ERRORLOG::ERRL_SEV_INFORMATIONAL)
{
TS_FAIL("testDistributeSmallMemAmt: Incorrect error log severity. Expected 0x%x, actual 0x%x", ERRORLOG::ERRL_SEV_INFORMATIONAL, l_errl->sev());
}
errlCommit(l_errl, CXXTEST_COMP_ID);
}
delete l_errl;
l_errl = nullptr;
}
uint64_t l_smfBarSize = 0;
l_errl = getSmfMemSize(l_smfBarSize);
if(l_errl)
{
TS_FAIL("testDistributeSmallMemAmt: could not getSmfMemSize");
errlCommit(l_errl, CXXTEST_COMP_ID);
break;
}
if(l_smfBarSize != SECUREBOOT::SMF::MIN_SMF_MEMORY_AMT)
{
TS_FAIL("testDistributeSmallMemAmt: Unexpected amount of memory allocated. Expected: 0x%x, actual: 0x%x", SECUREBOOT::SMF::MIN_SMF_MEMORY_AMT, l_smfBarSize);
}
} while(0);
TS_INFO(EXIT_MRK"testDistributeSmallMemAmt");
}
void testDistributeLargeMemAmt() const
{
TS_INFO(ENTER_MRK"testDistributeLargeMemAmt");
errlHndl_t l_errl = nullptr;
do {
if(!cv_isSmfLoaded)
{
break;
}
// Try to distribute very large amt of mem
l_errl = SECUREBOOT::SMF::distributeSmfMem(1000000*GIGABYTE);
if(!l_errl)
{
TS_FAIL("testDistributeLargeMemAmt: distributeSmfMem should have returned an error log.");
}
else
{
if(l_errl->reasonCode() != SECUREBOOT::RC_ALLOCATED_NE_REQUESTED)
{
TS_FAIL("testDistributeLargeMemAmt: Incorrect RC returned. Expected 0x%x, actual 0x%x", SECUREBOOT::RC_ALLOCATED_NE_REQUESTED, l_errl->reasonCode());
if(l_errl->sev() != ERRORLOG::ERRL_SEV_INFORMATIONAL)
{
TS_FAIL("testDistributeLargeMemAmt: Incorrect error log severity. Expected 0x%x, actual 0x%x", ERRORLOG::ERRL_SEV_INFORMATIONAL, l_errl->sev());
}
errlCommit(l_errl, CXXTEST_COMP_ID);
}
delete l_errl;
l_errl = nullptr;
}
uint64_t l_smfBarSize = 0;
l_errl = getSmfMemSize(l_smfBarSize);
if(l_errl)
{
TS_FAIL("testDistributeLargeMemAmt: could not getSmfMemSize");
errlCommit(l_errl, CXXTEST_COMP_ID);
break;
}
TARGETING::Target* l_masterProc = nullptr;
l_errl = TARGETING::targetService()
.queryMasterProcChipTargetHandle(l_masterProc);
if(l_errl)
{
TS_FAIL("testDistributeLargeMemAmt: could not get master proc!");
errlCommit(l_errl, CXXTEST_COMP_ID);
break;
}
uint64_t l_expectedMemAmt = getHighestPwrTwoLessThan(
SECUREBOOT::SMF::getTotalProcMemSize(l_masterProc) -
SECUREBOOT::SMF::MIN_MEM_RESERVED_FOR_HB);
if(l_smfBarSize != l_expectedMemAmt)
{
TS_FAIL("testDistributeLargeMemAmt: Unexpected amount of memory allocated. Expected: 0x%x, actual: 0x%x", l_expectedMemAmt, l_smfBarSize);
}
} while(0);
TS_INFO(EXIT_MRK"testDistributeLargeMemAmt");
}
void testDistributeExactAmt() const
{
TS_INFO(ENTER_MRK"testDistributeExactAmt");
errlHndl_t l_errl = nullptr;
do {
if(!cv_isSmfLoaded)
{
break;
}
// This amount should be distributed exactly
l_errl = SECUREBOOT::SMF::distributeSmfMem(DISTRIBUTE_EXACT_SMF_AMT);
if(l_errl)
{
TS_FAIL("testDistributeExactAmt: Unexpected error log returned from distributeSmfMem. Plid = 0x%x", l_errl->plid());
errlCommit(l_errl, CXXTEST_COMP_ID);
}
uint64_t l_smfBarSize = 0;
l_errl = getSmfMemSize(l_smfBarSize);
if(l_errl)
{
TS_FAIL("testDistributeExactAmt: could not getSmfMemSize");
errlCommit(l_errl, CXXTEST_COMP_ID);
break;
}
// Memory is distributed across processors so need to divide the
// expected results by the number of processors
TARGETING::TargetHandleList l_procList;
TARGETING::getAllChips(l_procList, TARGETING::TYPE_PROC, true);
if(l_smfBarSize != (DISTRIBUTE_EXACT_SMF_AMT/l_procList.size()))
{
TS_FAIL("testDistributeExactAmt: Unexpected amount of memory allocated. Expected: 0x%x, actual 0x%x", (DISTRIBUTE_EXACT_SMF_AMT/l_procList.size()), l_smfBarSize);
}
} while(0);
TS_INFO(EXIT_MRK"testDistributeExactAmt");
}
void testDistributeNotExactAmt()
{
TS_INFO(ENTER_MRK"testDistributeNotExactAmt");
errlHndl_t l_errl = nullptr;
do {
if(!cv_isSmfLoaded)
{
break;
}
// Distribute the amount that cannot be fit exactly.
// 800MB will be adjusted up to 1GB because of the power-of-two
// hardware constraints.
l_errl =
SECUREBOOT::SMF::distributeSmfMem(DISTRIBUTE_NON_EXACT_SMF_AMT);
if(!l_errl)
{
TS_FAIL("testDistributeNotExactAmt: distributeSmfMem should have returned an error log.");
}
else
{
if(l_errl->reasonCode() != SECUREBOOT::RC_ALLOCATED_NE_REQUESTED)
{
TS_FAIL("testDistributeNotExactAmt: Incorrect RC returned. Expected 0x%x, actual 0x%x", SECUREBOOT::RC_ALLOCATED_NE_REQUESTED, l_errl->reasonCode());
if(l_errl->sev() != ERRORLOG::ERRL_SEV_INFORMATIONAL)
{
TS_FAIL("testDistributeNotExactAmt: Incorrect error log severity. Expected 0x%x, actual 0x%x", ERRORLOG::ERRL_SEV_INFORMATIONAL, l_errl->sev());
}
errlCommit(l_errl, CXXTEST_COMP_ID);
}
delete l_errl;
l_errl = nullptr;
}
uint64_t l_smfBarSize = 0;
l_errl = getSmfMemSize(l_smfBarSize);
if(l_errl)
{
TS_FAIL("testDistributeNotExactAmt: could not getSmfMemSize");
errlCommit(l_errl, CXXTEST_COMP_ID);
break;
}
// Memory is distributed across processors so need to divide the
// expected results by the number of processors
TARGETING::TargetHandleList l_procList;
TARGETING::getAllChips(l_procList, TARGETING::TYPE_PROC, true);
if(l_smfBarSize != (DISTRIBUTE_EXACT_SMF_AMT/l_procList.size()))
{
TS_FAIL("testDistributeNotExactAmt: Unexpected amount of memory allocated. Expected: 0x%x, actual 0x%x", (DISTRIBUTE_EXACT_SMF_AMT/l_procList.size()), l_smfBarSize);
}
} while(0);
TS_INFO(EXIT_MRK"testDistributeNotExactAmt");
}
};
#endif
|