blob: 7c2a85f8e3c5f443a64fb646b06205c4577a57c5 [file] [log] [blame]
Lawrence Tangde9707f2022-07-19 10:54:31 +01001/**
Lawrence Tangefe17e22022-08-08 09:16:23 +01002 * Functions for generating pseudo-random CPER DMAr error sections.
Ed Tanousfedd4572024-07-12 13:56:00 -07003 *
Lawrence Tangde9707f2022-07-19 10:54:31 +01004 * Author: Lawrence.Tang@arm.com
5 **/
6
7#include <stdlib.h>
Thu Nguyene42fb482024-10-15 14:43:11 +00008#include <libcper/BaseTypes.h>
9#include <libcper/generator/gen-utils.h>
10#include <libcper/generator/sections/gen-section.h>
Lawrence Tangde9707f2022-07-19 10:54:31 +010011
Lawrence Tangefe17e22022-08-08 09:16:23 +010012//Generates a single pseudo-random generic DMAr error section, saving the resulting address to the given
Lawrence Tangde9707f2022-07-19 10:54:31 +010013//location. Returns the size of the newly created section.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080014size_t generate_section_dmar_generic(void **location,
15 GEN_VALID_BITS_TEST_TYPE validBitsType)
Lawrence Tangde9707f2022-07-19 10:54:31 +010016{
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080017 (void)validBitsType;
John Chungf8fc7052024-05-03 20:05:29 +080018 //Create random bytes.
19 int size = 32;
20 UINT8 *bytes = generate_random_bytes(size);
Lawrence Tangde9707f2022-07-19 10:54:31 +010021
John Chungf8fc7052024-05-03 20:05:29 +080022 //Set reserved areas to zero.
23 UINT64 *reserved = (UINT64 *)(bytes + 16);
24 *reserved = 0;
25 *(reserved + 1) = 0;
Lawrence Tangde9707f2022-07-19 10:54:31 +010026
John Chungf8fc7052024-05-03 20:05:29 +080027 //Set expected values.
28 *(bytes + 4) = rand() % 0xC; //Fault reason.
29 *(bytes + 5) = rand() % 2; //Access type.
30 *(bytes + 6) = rand() % 2; //Address type.
31 *(bytes + 7) = rand() % 2 + 1; //Architecture type.
32
33 //Set return values, exit.
34 *location = bytes;
35 return size;
Lawrence Tangde9707f2022-07-19 10:54:31 +010036}
37
Lawrence Tangefe17e22022-08-08 09:16:23 +010038//Generates a single pseudo-random VT-d DMAr error section, saving the resulting address to the given
Lawrence Tangde9707f2022-07-19 10:54:31 +010039//location. Returns the size of the newly created section.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080040size_t generate_section_dmar_vtd(void **location,
41 GEN_VALID_BITS_TEST_TYPE validBitsType)
Lawrence Tangde9707f2022-07-19 10:54:31 +010042{
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080043 (void)validBitsType;
John Chungf8fc7052024-05-03 20:05:29 +080044 //Create random bytes.
45 int size = 144;
46 UINT8 *bytes = generate_random_bytes(size);
Lawrence Tangde9707f2022-07-19 10:54:31 +010047
John Chungf8fc7052024-05-03 20:05:29 +080048 //Set reserved areas to zero.
49 for (int i = 0; i < 12; i++) {
50 *(bytes + 36 + i) = 0; //Reserved bytes 36-47.
51 }
52 UINT8 *fault_record = bytes + 48;
53 UINT32 *reserved = (UINT32 *)(fault_record);
54 *reserved &= ~0xFFF; //First 12 bits of fault record.
55 reserved = (UINT32 *)(fault_record + 8);
56 *reserved &= ~0x1FFF0000; //Bits 80-92 of fault record.
57 *(fault_record + 15) &= 0x7; //Very last bit of fault record.
58
59 //Set return values, exit.
60 *location = bytes;
61 return size;
Lawrence Tangde9707f2022-07-19 10:54:31 +010062}
63
Lawrence Tangefe17e22022-08-08 09:16:23 +010064//Generates a single pseudo-random IOMMU DMAr error section, saving the resulting address to the given
Lawrence Tangde9707f2022-07-19 10:54:31 +010065//location. Returns the size of the newly created section.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080066size_t generate_section_dmar_iommu(void **location,
67 GEN_VALID_BITS_TEST_TYPE validBitsType)
Lawrence Tangde9707f2022-07-19 10:54:31 +010068{
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080069 (void)validBitsType;
John Chungf8fc7052024-05-03 20:05:29 +080070 //Create random bytes.
71 int size = 144;
72 UINT8 *bytes = generate_random_bytes(size);
Lawrence Tangde9707f2022-07-19 10:54:31 +010073
John Chungf8fc7052024-05-03 20:05:29 +080074 //Set reserved areas to zero.
75 for (int i = 0; i < 7; i++) {
76 *(bytes + 1 + i) = 0; //Reserved bytes 1 to 7.
77 }
78 UINT64 *reserved = (UINT64 *)(bytes + 24);
79 *reserved = 0; //Reserved bytes 24-31.
80 for (int i = 0; i < 16; i++) {
81 *(bytes + 48 + i) = 0; //Reserved bytes 48-63.
82 }
83
84 //Set return values, exit.
85 *location = bytes;
86 return size;
87}