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> |
| 8 | #include "../../edk/BaseTypes.h" |
| 9 | #include "../gen-utils.h" |
Lawrence Tang | 8f97745 | 2022-08-24 14:55:07 +0100 | [diff] [blame] | 10 | #include "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. |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 14 | size_t generate_section_dmar_generic(void **location) |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 15 | { |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 16 | //Create random bytes. |
| 17 | int size = 32; |
| 18 | UINT8 *bytes = generate_random_bytes(size); |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 19 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 20 | //Set reserved areas to zero. |
| 21 | UINT64 *reserved = (UINT64 *)(bytes + 16); |
| 22 | *reserved = 0; |
| 23 | *(reserved + 1) = 0; |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 24 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 25 | //Set expected values. |
| 26 | *(bytes + 4) = rand() % 0xC; //Fault reason. |
| 27 | *(bytes + 5) = rand() % 2; //Access type. |
| 28 | *(bytes + 6) = rand() % 2; //Address type. |
| 29 | *(bytes + 7) = rand() % 2 + 1; //Architecture type. |
| 30 | |
| 31 | //Set return values, exit. |
| 32 | *location = bytes; |
| 33 | return size; |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 34 | } |
| 35 | |
Lawrence Tang | efe17e2 | 2022-08-08 09:16:23 +0100 | [diff] [blame] | 36 | //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] | 37 | //location. Returns the size of the newly created section. |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 38 | size_t generate_section_dmar_vtd(void **location) |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 39 | { |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 40 | //Create random bytes. |
| 41 | int size = 144; |
| 42 | UINT8 *bytes = generate_random_bytes(size); |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 43 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 44 | //Set reserved areas to zero. |
| 45 | for (int i = 0; i < 12; i++) { |
| 46 | *(bytes + 36 + i) = 0; //Reserved bytes 36-47. |
| 47 | } |
| 48 | UINT8 *fault_record = bytes + 48; |
| 49 | UINT32 *reserved = (UINT32 *)(fault_record); |
| 50 | *reserved &= ~0xFFF; //First 12 bits of fault record. |
| 51 | reserved = (UINT32 *)(fault_record + 8); |
| 52 | *reserved &= ~0x1FFF0000; //Bits 80-92 of fault record. |
| 53 | *(fault_record + 15) &= 0x7; //Very last bit of fault record. |
| 54 | |
| 55 | //Set return values, exit. |
| 56 | *location = bytes; |
| 57 | return size; |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 58 | } |
| 59 | |
Lawrence Tang | efe17e2 | 2022-08-08 09:16:23 +0100 | [diff] [blame] | 60 | //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] | 61 | //location. Returns the size of the newly created section. |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 62 | size_t generate_section_dmar_iommu(void **location) |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 63 | { |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 64 | //Create random bytes. |
| 65 | int size = 144; |
| 66 | UINT8 *bytes = generate_random_bytes(size); |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 67 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 68 | //Set reserved areas to zero. |
| 69 | for (int i = 0; i < 7; i++) { |
| 70 | *(bytes + 1 + i) = 0; //Reserved bytes 1 to 7. |
| 71 | } |
| 72 | UINT64 *reserved = (UINT64 *)(bytes + 24); |
| 73 | *reserved = 0; //Reserved bytes 24-31. |
| 74 | for (int i = 0; i < 16; i++) { |
| 75 | *(bytes + 48 + i) = 0; //Reserved bytes 48-63. |
| 76 | } |
| 77 | |
| 78 | //Set return values, exit. |
| 79 | *location = bytes; |
| 80 | return size; |
| 81 | } |