Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 1 | /** |
Lawrence Tang | efe17e2 | 2022-08-08 09:16:23 +0100 | [diff] [blame] | 2 | * Functions for generating pseudo-random CPER DMAr error sections. |
Ed Tanous | fedd457 | 2024-07-12 13:56:00 -0700 | [diff] [blame] | 3 | * |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 4 | * Author: Lawrence.Tang@arm.com |
| 5 | **/ |
| 6 | |
| 7 | #include <stdlib.h> |
Thu Nguyen | e42fb48 | 2024-10-15 14:43:11 +0000 | [diff] [blame] | 8 | #include <libcper/BaseTypes.h> |
| 9 | #include <libcper/generator/gen-utils.h> |
| 10 | #include <libcper/generator/sections/gen-section.h> |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 11 | |
Lawrence Tang | efe17e2 | 2022-08-08 09:16:23 +0100 | [diff] [blame] | 12 | //Generates a single pseudo-random generic DMAr error section, saving the resulting address to the given |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 13 | //location. Returns the size of the newly created section. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 14 | size_t generate_section_dmar_generic(void **location, |
| 15 | GEN_VALID_BITS_TEST_TYPE validBitsType) |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 16 | { |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 17 | (void)validBitsType; |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 18 | //Create random bytes. |
| 19 | int size = 32; |
| 20 | UINT8 *bytes = generate_random_bytes(size); |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 21 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 22 | //Set reserved areas to zero. |
| 23 | UINT64 *reserved = (UINT64 *)(bytes + 16); |
| 24 | *reserved = 0; |
| 25 | *(reserved + 1) = 0; |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 26 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 27 | //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 Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 36 | } |
| 37 | |
Lawrence Tang | efe17e2 | 2022-08-08 09:16:23 +0100 | [diff] [blame] | 38 | //Generates a single pseudo-random VT-d DMAr error section, saving the resulting address to the given |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 39 | //location. Returns the size of the newly created section. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 40 | size_t generate_section_dmar_vtd(void **location, |
| 41 | GEN_VALID_BITS_TEST_TYPE validBitsType) |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 42 | { |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 43 | (void)validBitsType; |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 44 | //Create random bytes. |
| 45 | int size = 144; |
| 46 | UINT8 *bytes = generate_random_bytes(size); |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 47 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 48 | //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 Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 62 | } |
| 63 | |
Lawrence Tang | efe17e2 | 2022-08-08 09:16:23 +0100 | [diff] [blame] | 64 | //Generates a single pseudo-random IOMMU DMAr error section, saving the resulting address to the given |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 65 | //location. Returns the size of the newly created section. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 66 | size_t generate_section_dmar_iommu(void **location, |
| 67 | GEN_VALID_BITS_TEST_TYPE validBitsType) |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 68 | { |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 69 | (void)validBitsType; |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 70 | //Create random bytes. |
| 71 | int size = 144; |
| 72 | UINT8 *bytes = generate_random_bytes(size); |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 73 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 74 | //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 | } |