veridian_kernel/
test_tasks.rs1#![allow(clippy::fn_to_numeric_cast, function_casts_as_integer)]
15
16use crate::sched;
17
18#[no_mangle]
20pub extern "C" fn test_task_a() -> ! {
21 #[cfg(target_arch = "aarch64")]
22 {
23 use crate::arch::aarch64::direct_uart::{direct_print_num, uart_write_str};
24
25 unsafe {
34 let mut counter = 0u64;
35
36 uart_write_str("[TASK A] Started\n");
37
38 loop {
39 uart_write_str("[TASK A] Running - count: ");
41 direct_print_num(counter);
42 uart_write_str("\n");
43
44 sched::yield_cpu();
46
47 counter = counter.wrapping_add(1);
49
50 core::arch::asm!("mov x0, #50000");
52 core::arch::asm!("mov x1, #10");
53 core::arch::asm!("2: mov x2, x0");
54 core::arch::asm!("1: sub x2, x2, #1");
55 core::arch::asm!("cbnz x2, 1b");
56 core::arch::asm!("sub x1, x1, #1");
57 core::arch::asm!("cbnz x1, 2b");
58 }
59 }
60 }
61
62 #[cfg(not(target_arch = "aarch64"))]
63 {
64 let mut counter = 0u64;
65
66 println!("[TASK A] Started");
67
68 loop {
69 println!("[TASK A] Running - count: {}", counter);
70
71 sched::yield_cpu();
73
74 counter = counter.wrapping_add(1);
75
76 for _ in 0..500000 {
78 core::hint::spin_loop();
79 }
80 }
81 }
82}
83
84#[no_mangle]
86pub extern "C" fn test_task_b() -> ! {
87 #[cfg(target_arch = "aarch64")]
88 {
89 use crate::arch::aarch64::direct_uart::{direct_print_num, uart_write_str};
90
91 unsafe {
95 let mut counter = 0u64;
96
97 uart_write_str("[TASK B] Started\n");
98
99 loop {
100 uart_write_str("[TASK B] Executing - value: ");
102 direct_print_num(counter);
103 uart_write_str("\n");
104
105 sched::yield_cpu();
107
108 counter = counter.wrapping_add(10);
110
111 core::arch::asm!("mov x0, #50000");
113 core::arch::asm!("mov x1, #15");
114 core::arch::asm!("2: mov x2, x0");
115 core::arch::asm!("1: sub x2, x2, #1");
116 core::arch::asm!("cbnz x2, 1b");
117 core::arch::asm!("sub x1, x1, #1");
118 core::arch::asm!("cbnz x1, 2b");
119 }
120 }
121 }
122
123 #[cfg(not(target_arch = "aarch64"))]
124 {
125 let mut counter = 0u64;
126
127 println!("[TASK B] Started");
128
129 loop {
130 println!("[TASK B] Executing - value: 0x{:x}", counter);
131
132 sched::yield_cpu();
134
135 counter = counter.wrapping_add(10);
136
137 for _ in 0..750000 {
139 core::hint::spin_loop();
140 }
141 }
142 }
143}
144
145pub fn create_test_tasks() {
147 #[cfg(feature = "alloc")]
148 {
149 use alloc::string::String;
150
151 use crate::process;
152
153 kprintln!("[TEST] Creating test tasks for context switch verification");
154
155 match process::lifecycle::create_process(String::from("test_task_a"), 0) {
157 Ok(_pid_a) => {
158 kprintln!("[TEST] Created process A");
159
160 if let Err(_e) = process::create_thread(test_task_a as usize, 0, 0, 0) {
161 kprintln!("[TEST] Failed to create thread for task A");
162 } else {
163 kprintln!("[TEST] Created thread for task A");
164 }
165 }
166 Err(_e) => {
167 kprintln!("[TEST] Failed to create task A");
168 }
169 }
170
171 match process::lifecycle::create_process(String::from("test_task_b"), 0) {
173 Ok(_pid_b) => {
174 kprintln!("[TEST] Created process B");
175
176 if let Err(_e) = process::create_thread(test_task_b as usize, 0, 0, 0) {
177 kprintln!("[TEST] Failed to create thread for task B");
178 } else {
179 kprintln!("[TEST] Created thread for task B");
180 }
181 }
182 Err(_e) => {
183 kprintln!("[TEST] Failed to create task B");
184 }
185 }
186
187 kprintln!("[TEST] Test tasks created successfully");
188 }
189
190 #[cfg(not(feature = "alloc"))]
191 {
192 kprintln!("[TEST] Cannot create test tasks: alloc feature not enabled");
193 }
194}