1 /*
2  * Copyright (c) 2020 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/ztest.h>
9 
10 #include <zephyr/drivers/virtualization/ivshmem.h>
11 
ZTEST(ivshmem,test_ivshmem_plain)12 ZTEST(ivshmem, test_ivshmem_plain)
13 {
14 	const struct device *ivshmem;
15 	uintptr_t mem;
16 	size_t size;
17 	uint32_t id;
18 	uint32_t *area_to_rw;
19 	uint16_t vectors;
20 	int ret;
21 
22 	ivshmem = DEVICE_DT_GET_ONE(qemu_ivshmem);
23 	zassert_true(device_is_ready(ivshmem), "ivshmem device is not ready");
24 
25 	size = ivshmem_get_mem(ivshmem, &mem);
26 	zassert_not_equal(size, 0, "Size cannot not be 0");
27 	zassert_not_null((void *)mem, "Shared memory cannot be null");
28 
29 	id = ivshmem_get_id(ivshmem);
30 	zassert_equal(id, 0, "ID should be 0 on ivshmem-plain");
31 
32 	area_to_rw = (uint32_t *)mem;
33 
34 	*area_to_rw = 8108; /* some data */
35 	zassert_equal(*area_to_rw, 8108,
36 		      "Could not r/w to the shared memory");
37 
38 	/* Quickly verifying that non-plain features return proper code */
39 	vectors = ivshmem_get_vectors(ivshmem);
40 	zassert_equal(vectors, 0, "ivshmem-plain cannot have vectors");
41 
42 	ret = z_impl_ivshmem_int_peer(ivshmem, 0, 0);
43 	zassert_equal(ret, -ENOSYS,
44 		      "interrupting peers should not be supported");
45 
46 	ret = ivshmem_register_handler(ivshmem, NULL, 0);
47 	zassert_equal(ret, -ENOSYS,
48 		      "registering handlers should not be supported");
49 }
50 
test_is_usermode(void)51 static void test_is_usermode(void)
52 {
53 	zassert_true(k_is_user_context(), "thread left in kernel mode");
54 }
55 
ZTEST(ivshmem,test_quit_kernel)56 ZTEST(ivshmem, test_quit_kernel)
57 {
58 #ifdef CONFIG_USERSPACE
59 	k_thread_user_mode_enter((k_thread_entry_t)test_is_usermode,
60 				 NULL, NULL, NULL);
61 #else
62 	ztest_test_skip();
63 #endif
64 }
65 
66 ZTEST_SUITE(ivshmem, NULL, NULL, NULL, NULL, NULL);
67