Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 1 | /** |
Lawrence Tang | efe17e2 | 2022-08-08 09:16:23 +0100 | [diff] [blame] | 2 | * Functions for generating pseudo-random CPER IA32/x64 sections. |
Ed Tanous | fedd457 | 2024-07-12 13:56:00 -0700 | [diff] [blame] | 3 | * |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 4 | * Author: Lawrence.Tang@arm.com |
| 5 | **/ |
| 6 | |
| 7 | #include <stdlib.h> |
| 8 | #include <string.h> |
Thu Nguyen | e42fb48 | 2024-10-15 14:43:11 +0000 | [diff] [blame] | 9 | #include <libcper/Cper.h> |
| 10 | #include <libcper/generator/gen-utils.h> |
| 11 | #include <libcper/generator/sections/gen-section.h> |
Lawrence Tang | 67cbed6 | 2022-07-18 16:44:51 +0100 | [diff] [blame] | 12 | #define IA32X64_ERROR_STRUCTURE_SIZE 64 |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 13 | |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 14 | void *generate_ia32x64_error_structure(GEN_VALID_BITS_TEST_TYPE validBitsType); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 15 | size_t generate_ia32x64_context_structure(void **location); |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 16 | |
Lawrence Tang | efe17e2 | 2022-08-08 09:16:23 +0100 | [diff] [blame] | 17 | //Generates a single pseudo-random IA32/x64 section, saving the resulting address to the given |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 18 | //location. Returns the size of the newly created section. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 19 | size_t generate_section_ia32x64(void **location, |
| 20 | GEN_VALID_BITS_TEST_TYPE validBitsType) |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 21 | { |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 22 | //Set up for generation of error/context structures. |
Ed Tanous | 2d4d3b6 | 2025-03-11 10:34:29 -0700 | [diff] [blame^] | 23 | UINT16 error_structure_num = cper_rand() % 4 + 1; |
| 24 | UINT16 context_structure_num = cper_rand() % 4 + 1; |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 25 | void *error_structures[error_structure_num]; |
| 26 | void *context_structures[context_structure_num]; |
| 27 | size_t context_structure_lengths[context_structure_num]; |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 28 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 29 | //Generate the structures. |
| 30 | for (int i = 0; i < error_structure_num; i++) { |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 31 | error_structures[i] = |
| 32 | generate_ia32x64_error_structure(validBitsType); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 33 | } |
| 34 | for (int i = 0; i < context_structure_num; i++) { |
| 35 | context_structure_lengths[i] = |
| 36 | generate_ia32x64_context_structure(context_structures + |
| 37 | i); |
| 38 | } |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 39 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 40 | //Create a valid IA32/x64 section. |
| 41 | size_t total_len = |
| 42 | 64 + (IA32X64_ERROR_STRUCTURE_SIZE * error_structure_num); |
| 43 | for (int i = 0; i < context_structure_num; i++) { |
| 44 | total_len += context_structure_lengths[i]; |
| 45 | } |
| 46 | UINT8 *section = generate_random_bytes(total_len); |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 47 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 48 | //Null extend the end of the CPUID in the header. |
| 49 | for (int i = 0; i < 16; i++) { |
| 50 | *(section + 48 + i) = 0x0; |
| 51 | } |
Lawrence Tang | aacf0e2 | 2022-07-20 13:28:52 +0100 | [diff] [blame] | 52 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 53 | //Set header information. |
| 54 | UINT64 *validation = (UINT64 *)section; |
| 55 | *validation &= 0x3; |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 56 | if (validBitsType == ALL_VALID) { |
| 57 | *validation = 0x3; |
| 58 | } else if (validBitsType == SOME_VALID) { |
| 59 | *validation = 0x2; |
| 60 | } |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 61 | *validation |= error_structure_num << 2; |
| 62 | *validation |= context_structure_num << 8; |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 63 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 64 | //Copy in structures, free resources. |
| 65 | UINT8 *cur_pos = section + 64; |
| 66 | for (int i = 0; i < error_structure_num; i++) { |
| 67 | memcpy(cur_pos, error_structures[i], |
| 68 | IA32X64_ERROR_STRUCTURE_SIZE); |
| 69 | free(error_structures[i]); |
| 70 | cur_pos += IA32X64_ERROR_STRUCTURE_SIZE; |
| 71 | } |
| 72 | for (int i = 0; i < context_structure_num; i++) { |
| 73 | memcpy(cur_pos, context_structures[i], |
| 74 | context_structure_lengths[i]); |
| 75 | free(context_structures[i]); |
| 76 | cur_pos += context_structure_lengths[i]; |
| 77 | } |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 78 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 79 | //Set return values, exist. |
| 80 | *location = section; |
| 81 | return total_len; |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | //Generates a single IA32/x64 error structure. Must later be freed. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 85 | void *generate_ia32x64_error_structure(GEN_VALID_BITS_TEST_TYPE validBitsType) |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 86 | { |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 87 | UINT8 *error_structure = |
| 88 | generate_random_bytes(IA32X64_ERROR_STRUCTURE_SIZE); |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 89 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 90 | //Set error structure reserved space to zero. |
| 91 | UINT64 *validation = (UINT64 *)(error_structure + 16); |
| 92 | *validation &= 0x1F; |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 93 | if (validBitsType == ALL_VALID) { |
| 94 | *validation = 0x1F; |
| 95 | } else if (validBitsType == SOME_VALID) { |
| 96 | *validation = 0x15; |
| 97 | } |
Lawrence Tang | aacf0e2 | 2022-07-20 13:28:52 +0100 | [diff] [blame] | 98 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 99 | //Create a random type of error structure. |
| 100 | EFI_GUID *guid = (EFI_GUID *)error_structure; |
| 101 | UINT64 *check_info = (UINT64 *)(error_structure + 24); |
Ed Tanous | 2d4d3b6 | 2025-03-11 10:34:29 -0700 | [diff] [blame^] | 102 | int error_structure_type = cper_rand() % 4; |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 103 | switch (error_structure_type) { |
| 104 | //Cache |
| 105 | case 0: |
| 106 | memcpy(guid, &gEfiIa32x64ErrorTypeCacheCheckGuid, |
| 107 | sizeof(EFI_GUID)); |
Lawrence Tang | aacf0e2 | 2022-07-20 13:28:52 +0100 | [diff] [blame] | 108 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 109 | //Set reserved space to zero. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 110 | *check_info = ~0x20FF00; |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 111 | *check_info &= 0x3FFFFFFF; |
| 112 | break; |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 113 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 114 | //TLB |
| 115 | case 1: |
| 116 | memcpy(guid, &gEfiIa32x64ErrorTypeTlbCheckGuid, |
| 117 | sizeof(EFI_GUID)); |
Lawrence Tang | aacf0e2 | 2022-07-20 13:28:52 +0100 | [diff] [blame] | 118 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 119 | //Set reserved space to zero. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 120 | *check_info = ~0x20FF00; |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 121 | *check_info &= 0x3FFFFFFF; |
| 122 | break; |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 123 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 124 | //Bus |
| 125 | case 2: |
| 126 | memcpy(guid, &gEfiIa32x64ErrorTypeBusCheckGuid, |
| 127 | sizeof(EFI_GUID)); |
Lawrence Tang | aacf0e2 | 2022-07-20 13:28:52 +0100 | [diff] [blame] | 128 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 129 | //Set reserved space to zero. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 130 | *check_info = ~0x20F800; |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 131 | *check_info &= 0x7FFFFFFFF; |
| 132 | break; |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 133 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 134 | //MS |
| 135 | case 3: |
| 136 | memcpy(guid, &gEfiIa32x64ErrorTypeMsCheckGuid, |
| 137 | sizeof(EFI_GUID)); |
Lawrence Tang | aacf0e2 | 2022-07-20 13:28:52 +0100 | [diff] [blame] | 138 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 139 | //Set reserved space to zero. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 140 | *check_info = ~0xFFC0; |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 141 | *check_info &= 0xFFFFFF; |
| 142 | break; |
| 143 | } |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 144 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 145 | return error_structure; |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | //Generates a single IA32/x64 context structure. Must later be freed. |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 149 | size_t generate_ia32x64_context_structure(void **location) |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 150 | { |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 151 | //Initial length is 16 bytes. Add extra based on type. |
Ed Tanous | 2d4d3b6 | 2025-03-11 10:34:29 -0700 | [diff] [blame^] | 152 | int reg_type = cper_rand() % 8; |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 153 | int reg_size = 0; |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 154 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 155 | //Set register size. |
| 156 | if (reg_type == 2) { |
Ed Tanous | 2d4d3b6 | 2025-03-11 10:34:29 -0700 | [diff] [blame^] | 157 | reg_size = 92; //IA32 registers. |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 158 | } else if (reg_type == 3) { |
Ed Tanous | 2d4d3b6 | 2025-03-11 10:34:29 -0700 | [diff] [blame^] | 159 | reg_size = 244; //x64 registers. |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 160 | } else { |
Ed Tanous | 2d4d3b6 | 2025-03-11 10:34:29 -0700 | [diff] [blame^] | 161 | reg_size = (cper_rand() % 5 + 1) * 32; //Not table defined. |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 162 | } |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 163 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 164 | //Create structure randomly. |
| 165 | int total_size = 16 + reg_size; |
| 166 | UINT16 *context_structure = (UINT16 *)generate_random_bytes(total_size); |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 167 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 168 | //If it is x64 registers, set reserved area accordingly. |
| 169 | if (reg_type == 3) { |
| 170 | UINT8 *reg_bytes = (UINT8 *)(context_structure + 8); |
| 171 | UINT32 *reserved = (UINT32 *)(reg_bytes + 140); |
| 172 | *reserved = 0; |
| 173 | } |
Lawrence Tang | 04750a9 | 2022-07-20 16:45:11 +0100 | [diff] [blame] | 174 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 175 | //Set header information. |
| 176 | *(context_structure) = reg_type; |
| 177 | *(context_structure + 1) = reg_size; |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 178 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 179 | //Set return values and exit. |
| 180 | *location = context_structure; |
| 181 | return total_size; |
| 182 | } |