Tom Rini | a0434ab | 2018-06-03 16:10:22 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Heinrich Schuchardt | 0073f26 | 2018-04-03 21:59:34 +0200 | [diff] [blame] | 2 | /* |
| 3 | * efi_selftest_unaligned |
| 4 | * |
| 5 | * Copyright (c) 2018 Heinrich Schuchardt <xypron.glpk@gmx.de> |
| 6 | * |
Heinrich Schuchardt | 0073f26 | 2018-04-03 21:59:34 +0200 | [diff] [blame] | 7 | * Test unaligned memory access on ARMv7. |
| 8 | */ |
| 9 | |
| 10 | #include <efi_selftest.h> |
| 11 | |
| 12 | struct aligned_buffer { |
| 13 | char a[8] __aligned(8); |
| 14 | }; |
| 15 | |
| 16 | /* |
Heinrich Schuchardt | 0aa5047 | 2022-05-01 11:24:22 +0200 | [diff] [blame] | 17 | * Return an u32 at a given address. |
Heinrich Schuchardt | 0073f26 | 2018-04-03 21:59:34 +0200 | [diff] [blame] | 18 | * If the address is not four byte aligned, an unaligned memory access |
| 19 | * occurs. |
| 20 | * |
| 21 | * @addr: address to read |
Heinrich Schuchardt | fbe9021 | 2022-01-20 19:48:20 +0100 | [diff] [blame] | 22 | * Return: value at the address |
Heinrich Schuchardt | 0073f26 | 2018-04-03 21:59:34 +0200 | [diff] [blame] | 23 | */ |
Heinrich Schuchardt | 0aa5047 | 2022-05-01 11:24:22 +0200 | [diff] [blame] | 24 | static inline u32 deref(void *addr) |
Heinrich Schuchardt | 0073f26 | 2018-04-03 21:59:34 +0200 | [diff] [blame] | 25 | { |
| 26 | int ret; |
| 27 | |
| 28 | asm( |
| 29 | "ldr %[out], [%[in]]\n\t" |
| 30 | : [out] "=r" (ret) |
| 31 | : [in] "r" (addr) |
| 32 | ); |
| 33 | return ret; |
| 34 | } |
| 35 | |
| 36 | /* |
| 37 | * Execute unit test. |
| 38 | * An unaligned memory access is executed. The result is checked. |
| 39 | * |
Heinrich Schuchardt | fbe9021 | 2022-01-20 19:48:20 +0100 | [diff] [blame] | 40 | * Return: EFI_ST_SUCCESS for success |
Heinrich Schuchardt | 0073f26 | 2018-04-03 21:59:34 +0200 | [diff] [blame] | 41 | */ |
| 42 | static int execute(void) |
| 43 | { |
| 44 | struct aligned_buffer buf = { |
| 45 | {0, 1, 2, 3, 4, 5, 6, 7}, |
Heinrich Schuchardt | 0aa5047 | 2022-05-01 11:24:22 +0200 | [diff] [blame] | 46 | }; |
Heinrich Schuchardt | 0073f26 | 2018-04-03 21:59:34 +0200 | [diff] [blame] | 47 | u32 r = 0; |
| 48 | |
| 49 | /* Read an unaligned address */ |
Heinrich Schuchardt | 0aa5047 | 2022-05-01 11:24:22 +0200 | [diff] [blame] | 50 | r = deref(&buf.a[1]); |
Heinrich Schuchardt | 0073f26 | 2018-04-03 21:59:34 +0200 | [diff] [blame] | 51 | |
| 52 | /* UEFI only supports low endian systems */ |
| 53 | if (r != 0x04030201) { |
| 54 | efi_st_error("Unaligned access failed"); |
| 55 | return EFI_ST_FAILURE; |
| 56 | } |
| 57 | |
| 58 | return EFI_ST_SUCCESS; |
| 59 | } |
| 60 | |
| 61 | EFI_UNIT_TEST(unaligned) = { |
| 62 | .name = "unaligned memory access", |
| 63 | .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT, |
| 64 | .execute = execute, |
| 65 | }; |