Skip to content

Commit d8ac658

Browse files
Jordan Yatescarlescufi
authored andcommitted
tests: update expected exception codes
Update expected exception codes for Cortex-M, Cortex-R and Cortex-A CPUs. Signed-off-by: Jordan Yates <[email protected]>
1 parent 35e78c4 commit d8ac658

File tree

3 files changed

+62
-17
lines changed

3 files changed

+62
-17
lines changed

tests/arch/arm/arm_interrupt/src/arm_interrupt.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,13 @@ ZTEST(arm_interrupt, test_arm_esf_collection)
166166
expected_msp = __get_MSP();
167167

168168
run_esf_validation = 1;
169+
#if defined(CONFIG_CPU_AARCH32_CORTEX_R) || defined(CONFIG_CPU_AARCH32_CORTEX_A)
170+
expected_reason = K_ERR_ARM_UNDEFINED_INSTRUCTION;
171+
#elif defined(CONFIG_ARMV7_M_ARMV8_M_MAINLINE)
172+
expected_reason = K_ERR_ARM_USAGE_UNDEFINED_INSTRUCTION;
173+
#else
169174
expected_reason = K_ERR_CPU_EXCEPTION;
175+
#endif
170176

171177
/* Run test thread and main thread at same priority to guarantee the
172178
* crashy thread we create below runs to completion before we get
@@ -468,7 +474,7 @@ ZTEST(arm_interrupt, test_arm_null_pointer_exception)
468474

469475
struct test_struct *test_struct_null_pointer = 0x0;
470476

471-
expected_reason = K_ERR_CPU_EXCEPTION;
477+
expected_reason = K_ERR_ARM_MEM_DATA_ACCESS;
472478

473479
printk("Reading a null pointer value: 0x%0x\n",
474480
test_struct_null_pointer->val[1]);

tests/kernel/fatal/exception/src/main.c

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ static struct k_thread alt_thread;
4545
volatile int rv;
4646

4747
static ZTEST_DMEM volatile int expected_reason = -1;
48+
static ZTEST_DMEM volatile int alternate_reason = -1;
4849

4950
void k_sys_fatal_error_handler(unsigned int reason, const z_arch_esf_t *pEsf)
5051
{
@@ -60,18 +61,34 @@ void k_sys_fatal_error_handler(unsigned int reason, const z_arch_esf_t *pEsf)
6061
k_fatal_halt(reason);
6162
}
6263

63-
if (reason != expected_reason) {
64-
printk("Wrong crash type got %d expected %d\n", reason,
65-
expected_reason);
64+
if ((reason != expected_reason) && (reason != alternate_reason)) {
65+
if (alternate_reason != -1) {
66+
printk("Wrong crash type got %d expected %d or %d\n", reason,
67+
expected_reason, alternate_reason);
68+
} else {
69+
printk("Wrong crash type got %d expected %d\n", reason,
70+
expected_reason);
71+
}
6672
k_fatal_halt(reason);
6773
}
6874

6975
expected_reason = -1;
76+
alternate_reason = -1;
7077
}
7178

7279
void entry_cpu_exception(void *p1, void *p2, void *p3)
7380
{
81+
#if defined(CONFIG_CPU_AARCH32_CORTEX_R) || defined(CONFIG_CPU_AARCH32_CORTEX_A)
82+
expected_reason = K_ERR_ARM_UNDEFINED_INSTRUCTION;
83+
#elif defined(CONFIG_ARMV7_M_ARMV8_M_MAINLINE)
84+
/* The generated exception depends on whether address 0 is valid and it is executed.
85+
* It is not feasible to generically check that here, so accept either faulting reason.
86+
*/
87+
expected_reason = K_ERR_ARM_USAGE_ILLEGAL_EPSR;
88+
alternate_reason = K_ERR_ARM_MEM_INSTRUCTION_ACCESS;
89+
#else
7490
expected_reason = K_ERR_CPU_EXCEPTION;
91+
#endif
7592

7693
#if defined(CONFIG_X86)
7794
__asm__ volatile ("ud2");
@@ -96,7 +113,11 @@ void entry_cpu_exception(void *p1, void *p2, void *p3)
96113

97114
void entry_cpu_exception_extend(void *p1, void *p2, void *p3)
98115
{
116+
#if defined(CONFIG_CPU_AARCH32_CORTEX_R) || defined(CONFIG_CPU_AARCH32_CORTEX_A)
117+
expected_reason = K_ERR_ARM_DEBUG_EVENT;
118+
#else
99119
expected_reason = K_ERR_CPU_EXCEPTION;
120+
#endif
100121

101122
#if defined(CONFIG_ARM64)
102123
__asm__ volatile ("svc 0");

tests/kernel/mem_protect/userspace/src/main.c

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@
2727
extern void arm_core_mpu_disable(void);
2828
#endif
2929

30+
#if defined(CONFIG_CPU_AARCH32_CORTEX_R) || defined(CONFIG_CPU_AARCH32_CORTEX_A)
31+
#define PERMISSION_EXCEPTION K_ERR_ARM_PERMISSION_FAULT
32+
#elif defined(CONFIG_ARMV7_M_ARMV8_M_MAINLINE)
33+
#define PERMISSION_EXCEPTION K_ERR_ARM_MEM_DATA_ACCESS
34+
#else
35+
#define PERMISSION_EXCEPTION K_ERR_CPU_EXCEPTION
36+
#endif
37+
3038
#define INFO(fmt, ...) printk(fmt, ##__VA_ARGS__)
3139
#define PIPE_LEN 1
3240
#define BYTES_TO_READ_WRITE 1
@@ -151,7 +159,11 @@ ZTEST_USER(userspace, test_write_control)
151159
#else
152160
uint32_t val;
153161

162+
#if defined(CONFIG_CPU_AARCH32_CORTEX_R) || defined(CONFIG_CPU_AARCH32_CORTEX_A)
163+
set_fault(K_ERR_ARM_UNDEFINED_INSTRUCTION);
164+
#else
154165
set_fault(K_ERR_CPU_EXCEPTION);
166+
#endif
155167

156168
val = __get_SCTLR();
157169
val |= SCTLR_DZ_Msk;
@@ -218,7 +230,13 @@ ZTEST_USER(userspace, test_disable_mmu_mpu)
218230

219231
#elif defined(CONFIG_ARM)
220232
#ifndef CONFIG_TRUSTED_EXECUTION_NONSECURE
233+
#if defined(CONFIG_CPU_AARCH32_CORTEX_R) || defined(CONFIG_CPU_AARCH32_CORTEX_A)
234+
set_fault(K_ERR_ARM_UNDEFINED_INSTRUCTION);
235+
#elif defined(CONFIG_ARMV7_M_ARMV8_M_MAINLINE)
236+
set_fault(K_ERR_ARM_BUS_PRECISE_DATA_BUS);
237+
#else
221238
set_fault(K_ERR_CPU_EXCEPTION);
239+
#endif
222240

223241
arm_core_mpu_disable();
224242
#else
@@ -258,7 +276,7 @@ ZTEST_USER(userspace, test_read_kernram)
258276
/* Try to read from kernel RAM. */
259277
void *p;
260278

261-
set_fault(K_ERR_CPU_EXCEPTION);
279+
set_fault(PERMISSION_EXCEPTION);
262280

263281
p = _current->init_data;
264282
printk("%p\n", p);
@@ -273,7 +291,7 @@ ZTEST_USER(userspace, test_read_kernram)
273291
ZTEST_USER(userspace, test_write_kernram)
274292
{
275293
/* Try to write to kernel RAM. */
276-
set_fault(K_ERR_CPU_EXCEPTION);
294+
set_fault(PERMISSION_EXCEPTION);
277295

278296
_current->init_data = NULL;
279297
zassert_unreachable("Write to kernel RAM did not fault");
@@ -308,7 +326,7 @@ ZTEST_USER(userspace, test_write_kernro)
308326
zassert_true(in_rodata,
309327
"_k_neg_eagain is not in rodata");
310328

311-
set_fault(K_ERR_CPU_EXCEPTION);
329+
set_fault(PERMISSION_EXCEPTION);
312330

313331
_k_neg_eagain = -EINVAL;
314332
zassert_unreachable("Write to kernel RO did not fault");
@@ -322,7 +340,7 @@ ZTEST_USER(userspace, test_write_kernro)
322340
ZTEST_USER(userspace, test_write_kerntext)
323341
{
324342
/* Try to write to kernel text. */
325-
set_fault(K_ERR_CPU_EXCEPTION);
343+
set_fault(PERMISSION_EXCEPTION);
326344

327345
memset(&z_is_thread_essential, 0, 4);
328346
zassert_unreachable("Write to kernel text did not fault");
@@ -337,7 +355,7 @@ static int kernel_data;
337355
*/
338356
ZTEST_USER(userspace, test_read_kernel_data)
339357
{
340-
set_fault(K_ERR_CPU_EXCEPTION);
358+
set_fault(PERMISSION_EXCEPTION);
341359

342360
printk("%d\n", kernel_data);
343361
zassert_unreachable("Read from data did not fault");
@@ -350,7 +368,7 @@ ZTEST_USER(userspace, test_read_kernel_data)
350368
*/
351369
ZTEST_USER(userspace, test_write_kernel_data)
352370
{
353-
set_fault(K_ERR_CPU_EXCEPTION);
371+
set_fault(PERMISSION_EXCEPTION);
354372

355373
kernel_data = 1;
356374
zassert_unreachable("Write to data did not fault");
@@ -383,7 +401,7 @@ ZTEST_USER(userspace, test_read_priv_stack)
383401
#else
384402
#error "Not implemented for this architecture"
385403
#endif
386-
set_fault(K_ERR_CPU_EXCEPTION);
404+
set_fault(PERMISSION_EXCEPTION);
387405

388406
printk("%c\n", *priv_stack_ptr);
389407
zassert_unreachable("Read from privileged stack did not fault");
@@ -407,7 +425,7 @@ ZTEST_USER(userspace, test_write_priv_stack)
407425
#else
408426
#error "Not implemented for this architecture"
409427
#endif
410-
set_fault(K_ERR_CPU_EXCEPTION);
428+
set_fault(PERMISSION_EXCEPTION);
411429

412430
*priv_stack_ptr = 42;
413431
zassert_unreachable("Write to privileged stack did not fault");
@@ -472,7 +490,7 @@ static void uthread_read_body(void *p1, void *p2, void *p3)
472490
{
473491
unsigned int *vptr = p1;
474492

475-
set_fault(K_ERR_CPU_EXCEPTION);
493+
set_fault(PERMISSION_EXCEPTION);
476494
printk("%u\n", *vptr);
477495
zassert_unreachable("Read from other thread stack did not fault");
478496
}
@@ -481,7 +499,7 @@ static void uthread_write_body(void *p1, void *p2, void *p3)
481499
{
482500
unsigned int *vptr = p1;
483501

484-
set_fault(K_ERR_CPU_EXCEPTION);
502+
set_fault(PERMISSION_EXCEPTION);
485503
*vptr = 2U;
486504
zassert_unreachable("Write to other thread stack did not fault");
487505
}
@@ -691,7 +709,7 @@ ZTEST(userspace_domain, test_1st_init_and_access_other_memdomain)
691709
* contains default_bool. This should fault when we try to write it.
692710
*/
693711
k_mem_domain_add_thread(&alternate_domain, k_current_get());
694-
set_fault(K_ERR_CPU_EXCEPTION);
712+
set_fault(PERMISSION_EXCEPTION);
695713
spawn_user(&default_bool);
696714
}
697715

@@ -742,7 +760,7 @@ ZTEST(userspace_domain, test_domain_remove_part_drop_to_user)
742760
/* We added alt_part to the default domain in the previous test,
743761
* remove it, and then try to access again.
744762
*/
745-
set_fault(K_ERR_CPU_EXCEPTION);
763+
set_fault(PERMISSION_EXCEPTION);
746764

747765
zassert_equal(
748766
k_mem_domain_remove_partition(&k_mem_domain_default, &alt_part),
@@ -792,7 +810,7 @@ ZTEST(userspace_domain_ctx, test_domain_remove_part_context_switch)
792810
/* We added alt_part to the default domain in the previous test,
793811
* remove it, and then try to access again.
794812
*/
795-
set_fault(K_ERR_CPU_EXCEPTION);
813+
set_fault(PERMISSION_EXCEPTION);
796814

797815
zassert_equal(
798816
k_mem_domain_remove_partition(&k_mem_domain_default, &alt_part),

0 commit comments

Comments
 (0)